473,770 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

rounding to 9's

i'm trying to round my currency string to end in 9. it's for a
pricing application.
this is the function i'm using to get the item in currency:
FormatCurrency( BoxCost, , , , TriState.True)

if an item is 41.87 i want the application to bring it up to 41.89

if an item is 41.84 i want the application to round down to 41.79

does anyone know how i could do this?

Jul 6 '07 #1
18 2231
On Jul 6, 4:06 pm, jdrott1 <jonathandr...@ gmail.comwrote:
i'm trying to round my currency string to end in 9. it's for a
pricing application.
this is the function i'm using to get the item in currency:
FormatCurrency( BoxCost, , , , TriState.True)

if an item is 41.87 i want the application to bring it up to 41.89

if an item is 41.84 i want the application to round down to 41.79

does anyone know how i could do this?
You would have to do it manually - just parse the second digit after
the decimal and adjust the value accordingly.

Thanks,

Seth Rowe
Jul 6 '07 #2
Hi Jonathan -

Have you tried rounding to the nearest tenth and subtracting one
hundreth?

Dim myVal As Double
myVal = 41.87
myVal = Math.Round(myVa l, 1) - 0.01
Debug.WriteLine (myVal) 'Returns 41.89

myVal = 41.84
myVal = Math.Round(myVa l, 1) - 0.01
Debug.WriteLine (myVal) 'Returns 41.79

....sometimes the simplest approaches work the best.

Happy Coding,

-Mark

Jul 6 '07 #3
On Jul 6, 3:50 pm, "Mark S. Milley, MCSD (BinarySwitch)"
<mark.mil...@bi naryswitch.comw rote:
Hi Jonathan -

Have you tried rounding to the nearest tenth and subtracting one
hundreth?

Dim myVal As Double
myVal = 41.87
myVal = Math.Round(myVa l, 1) - 0.01
Debug.WriteLine (myVal) 'Returns 41.89

myVal = 41.84
myVal = Math.Round(myVa l, 1) - 0.01
Debug.WriteLine (myVal) 'Returns 41.79

...sometimes the simplest approaches work the best.

Happy Coding,

-Mark
i'll give it a try. thanks.

Jul 6 '07 #4
Hi Joregen -

How are you getting 4 down and 5 up? Last time I checked, we were
using base ten.

Numbers ending in 0-4 are rounded down, 5-9 are rounded up; 5 on each
side.

Thanks,

-Mark

Jul 7 '07 #5
"jdrott1" <jo***********@ gmail.comwrote in message
news:11******** ************@m3 6g2000hse.googl egroups.com...
i'm trying to round my currency string to end in 9. it's for a
pricing application.
this is the function i'm using to get the item in currency:
FormatCurrency( BoxCost, , , , TriState.True)

if an item is 41.87 i want the application to bring it up to 41.89

if an item is 41.84 i want the application to round down to 41.79

does anyone know how i could do this?
Set two decimal places correcting the error in the Int function for proper
mathematical rounding we have a very simple function:
X = Int(10*(X+0.005 ))/10

Now set one decimal place mathematical and subtract 1c:
X = Int(10 * (X + 0.06)) / 10 - 0.01
Here, 4 rounds up to 9 as per 5 rounding up to 10
& 3 rounds down to 9 as per 4 rounding down to 0
In the correct mathematical method:
IE +1 inside the brackets compensates -1 outside
just as *10 inside the brackets balances /10 outside.

THUS:

Public Function Nanofy(X as Double) as Double
Nanofy = Int(10 * (X + 0.06)) / 10 - 0.01
End Function

Executing:
Dim x=MsgBox("The Value is: $", CStr(Nanofy(CDb le(InputBox(_
"Enter a Value","Enter a Value")))),MsgB oxStyle.OkOnly, "The answer is")

Nanofy(41.84) Returns 41.89
Nanofy(41.83) Returns 41.79
To bias rounding upwards,
simply increment the inner bracket sum in isolation.

SO:
Public Function Nanofy(X as Double) as Double
Nanofy = Int(10 * (X + 0.05)) / 10 - 0.01
End Function

Thus executing:
Dim x=MsgBox("The Value is: $", CStr(Nanofy(CDb le(InputBox(_
"Enter a Value","Enter a Value")))),MsgB oxStyle.OkOnly, "The answer is")

Nanofy(41.85) Returns 41.89
Nanofy(41.84) Returns 41.79

Good luck...

--
Timothy Casey GPEMC! >11950 is the nu****@fieldcra ft.com.au 2email
Terms & conditions apply. See www.fieldcraft.biz/GPEMC
Discover valid interoperable web menus, IE security, TSR Control,
& the most advanced speed reading application @ www.fieldcraft.biz
Jul 8 '07 #6
"Number 11950 - GPEMC! Replace number with 11950" <nu****@fieldcr aft.biz>
wrote in message
news:46******** *************** @lon-reader.news.tel stra.net...

ERRATA:

[SNIP]
Set two decimal places correcting the error in the Int function for proper
mathematical rounding we have a very simple function:
X = Int(10*(X+0.005 ))/10
[SNIP]

This should read:

Set ONE decimal place correcting the error in the Int function for proper
mathematical rounding we have a very simple function:
X = Int(10*(X+0.005 ))/10

Cheers

--
Timothy Casey GPEMC! >11950 is the nu****@fieldcra ft.com.au 2email
Terms & conditions apply. See www.fieldcraft.biz/GPEMC
Discover valid interoperable web menus, IE security, TSR Control,
& the most advanced speed reading application @ www.fieldcraft.biz
Jul 8 '07 #7
Yeah, that was definately much easier than my solution.

So... In what instance would this give a different result than mine?

Apparently Blind,

-Mark

Jul 8 '07 #8
On Sat, 07 Jul 2007 14:46:36 -0700, "Mark S. Milley, MCSD
(BinarySwitch)" <ma*********@bi naryswitch.comw rote:
>Hi Joregen -

How are you getting 4 down and 5 up? Last time I checked, we were
using base ten.

Numbers ending in 0-4 are rounded down, 5-9 are rounded up; 5 on each
side.

Thanks,

-Mark
I said 5 down, 4 up, and the last could go either way.

Because you are using simple Math.Round, which will round off 5s
to the nearest *even* value, e.g. 2.5 becomes 2, 3.5 becomes 4,
4.5 becomes 4, 5.5 becomes 6, and so on. That is great if that
is what you want, but I presumed it did not apply to the original
poster's needs.

Here is your suggestion wrapped in a test program:
---snip---
Private Sub RoundingTest()
Dim ups As Integer = 0
Dim downs As Integer = 0
Dim totalcount As Integer = 0
For value As Decimal = 1D To 1.99D Step 0.01D
Dim roundedvalue As Decimal = Math.Round(valu e, 1) - 0.01D
Debug.WriteLine (value.ToString ("0.00") & ": " &
roundedvalue.To String("0.00"))
totalcount = totalcount + 1
If roundedvalue < value Then
downs = downs + 1
Else
ups = ups + 1
End If
Next

Debug.WriteLine ("------------------------")
Debug.WriteLine ("Total: " & totalcount.ToSt ring)
Debug.WriteLine ("Ups: " & ups.ToString)
Debug.WriteLine ("Downs: " & downs.ToString)

End Sub
---snip---

The results are below: 55 values rounded down, 45 rounded
up.

You can read about various rounding methods here:
http://en.wikipedia.org/wiki/Rounding

Go watch Superman III for another lesson in rounding.

Regards,

Joergen Bech

---snip---
1,00: 0,99
1,01: 0,99
1,02: 0,99
1,03: 0,99
1,04: 0,99
1,05: 0,99
1,06: 1,09
1,07: 1,09
1,08: 1,09
1,09: 1,09
1,10: 1,09
1,11: 1,09
1,12: 1,09
1,13: 1,09
1,14: 1,09
1,15: 1,19
1,16: 1,19
1,17: 1,19
1,18: 1,19
1,19: 1,19
1,20: 1,19
1,21: 1,19
1,22: 1,19
1,23: 1,19
1,24: 1,19
1,25: 1,19
1,26: 1,29
1,27: 1,29
1,28: 1,29
1,29: 1,29
1,30: 1,29
1,31: 1,29
1,32: 1,29
1,33: 1,29
1,34: 1,29
1,35: 1,39
1,36: 1,39
1,37: 1,39
1,38: 1,39
1,39: 1,39
1,40: 1,39
1,41: 1,39
1,42: 1,39
1,43: 1,39
1,44: 1,39
1,45: 1,39
1,46: 1,49
1,47: 1,49
1,48: 1,49
1,49: 1,49
1,50: 1,49
1,51: 1,49
1,52: 1,49
1,53: 1,49
1,54: 1,49
1,55: 1,59
1,56: 1,59
1,57: 1,59
1,58: 1,59
1,59: 1,59
1,60: 1,59
1,61: 1,59
1,62: 1,59
1,63: 1,59
1,64: 1,59
1,65: 1,59
1,66: 1,69
1,67: 1,69
1,68: 1,69
1,69: 1,69
1,70: 1,69
1,71: 1,69
1,72: 1,69
1,73: 1,69
1,74: 1,69
1,75: 1,79
1,76: 1,79
1,77: 1,79
1,78: 1,79
1,79: 1,79
1,80: 1,79
1,81: 1,79
1,82: 1,79
1,83: 1,79
1,84: 1,79
1,85: 1,79
1,86: 1,89
1,87: 1,89
1,88: 1,89
1,89: 1,89
1,90: 1,89
1,91: 1,89
1,92: 1,89
1,93: 1,89
1,94: 1,89
1,95: 1,99
1,96: 1,99
1,97: 1,99
1,98: 1,99
1,99: 1,99
------------------------
Total: 100
Ups: 45
Downs: 55


Jul 8 '07 #9
"Mark S. Milley, MCSD (BinarySwitch)" <ma*********@bi naryswitch.comw rote
in message news:11******** *************@k 79g2000hse.goog legroups.com...
Yeah, that was definately much easier than my solution.

So... In what instance would this give a different result than mine?

Apparently Blind,

-Mark
I don't know about easier. Just a different way of getting to the same
place.
Also, sometimes doing the math helps centre one's perspective. I call it
'simple' because it's nothing like an algorithm for nailing the trig
function bugs in a 3D polar rotation. Normally a fairly strightforward piece
of math that on paper produces a single formula; The coding requires four
separate formulas because the canned trig functions cannot even correctly
sign the appropriate quadrant. As you can imagine, I'm a little cautious of
invoking canned subroutines because:

1. I don't know what's in them,
2. Sometimes they are slower than the derived subroutine or function,
3. Occasionally they are just plain old dead wrong.

I have to admit, I'd be disappointed if there was any difference in output,
or if your example was slower than mine. Your example did one thing for me,
and that was to emphasise the fact that there are many more improved
functions than I realised. However, reworking canned functions is good
practice for when there is no canned function or method to call on...

By the way, the debug.writeline (myfunction(myv ariable)) doesn't seem to work
the way it used to as debug.print(myf unction(myvaria ble)) did in VB6. Am I
missing a setting or reference?

--
Timothy Casey GPEMC! >11950 is the nu****@fieldcra ft.com.au 2email
Terms & conditions apply. See www.fieldcraft.biz/GPEMC
Discover valid interoperable web menus, IE security, TSR Control,
& the most advanced speed reading application @ www.fieldcraft.biz
Jul 8 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6741
by: Norvin Laudon | last post by:
Hi, Can somebody explain the following, from the MSDN documentation for the "System.Convert.ToInt32(double)" function <quote> Return Value value rounded to the nearest 32-bit signed integer. If value is halfway between two whole numbers, the even number is returned; that is, 4.5 is
4
7833
by: spebola | last post by:
I am using vb.net 2003 professional and I get the following results when using the round method: dim Amount as decimal = 180.255 Amount = Amount.Round(Amount, 2) Amount now contains 180.25. I need it to contain 180.26. Any ideas?
8
2086
by: Zorpiedoman | last post by:
Howcome: Dim D as decimal = .5D msgbox d.Round(D, 0) this returns "0" Now when I went to school .5 rounds UP to 1 not DOWN to zero?????!!! Documentation says this, but what the heck are they thinking??? I just don't
2
2642
by: Jiri Nemec | last post by:
Hello all, I have got one table with rounding values, table contains prices and round types. id price_from price_to rounding 1 0 1500 0.1 2 1500 5000 1 3 5000 15000 10 4 15000 0 100
11
6657
by: cj | last post by:
Lets assume all calculations are done with decimal data types so things are as precise as possible. When it comes to the final rounding to cut a check to pay dividends for example in VB rounding seems to be done like this 3.435 = 3.44 3.445 = 3.44 Dim decNbr1 As Decimal
5
8013
by: Spoon | last post by:
Hello everyone, I don't understand how the lrint() function works. long lrint(double x); The function returns the nearest long integer to x, consistent with the current rounding mode. It raises an invalid floating-point exception if the magnitude of the rounded value is too large to represent. And it raises an inexact floating-point exception if the return value does not
206
13308
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) It then updates the value with numberOfPrecisions after the decimal
20
5014
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
30
29686
by: bdsatish | last post by:
The built-in function round( ) will always "round up", that is 1.5 is rounded to 2.0 and 2.5 is rounded to 3.0. If I want to round to the nearest even, that is my_round(1.5) = 2 # As expected my_round(2.5) = 2 # Not 3, which is an odd num I'm interested in rounding numbers of the form "x.5" depending upon whether x is odd or even. Any idea about how to implement it ?
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9453
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8929
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.