
Sep 10th, 2021, 10:59 AM
#1
Thread Starter
New Member
Rounding a 4 decimal place number to 2 decimal places
I have the following problem:
I have a 4 decimal place number e.g. 14.5548 that I want rounded up correctly to 2 decimal places. So the correct rounded number in this example should be 14.56
Using format(amt, "0.00") produces 14.55
Using round(amt,2) produces 14.55
The following code produces the correct answer of 14.56 but is this how to do it or am I missing something?
round(round(amt,3),2)
Thanks for any advice.

Sep 10th, 2021, 11:06 AM
#2
Re: Rounding a 4 decimal place number to 2 decimal places
Originally Posted by nealb
I have the following problem:
I have a 4 decimal place number e.g. 14.5548 that I want rounded up correctly to 2 decimal places. So the correct rounded number in this example should be 14.56
The correct rounding is 14.55, that's the issue.
Because 0.0048 is less than half of 0.01.
Originally Posted by nealb
The following code produces the correct answer of 14.56 but is this how to do it or am I missing something?
round(round(amt,3),2)
That's because it first rounds to 14.555 and 0.005 being in the middle is then rounded up in the second call.

Sep 10th, 2021, 11:10 AM
#3
Re: Rounding a 4 decimal place number to 2 decimal places
Define "correctly."
Decades ago: Q196652: HOWTO: Implement Custom Rounding Procedures
SUMMARY
There are a number of different rounding algorithms available in Microsoft products. Rounding algorithms range from Arithmetic Rounding in Excel's Worksheet Round() function to Banker's Rounding in the CInt(), CLng(), and Round() functions in Visual Basic for Applications. This article describes what the various Visual Basic for Applications rounding functions do and provides samples of using the functions. In addition, the article includes sample functions that implement various rounding algorithms.

Sep 10th, 2021, 11:22 AM
#4
Re: Rounding a 4 decimal place number to 2 decimal places
WOW, I don't care what any random webpage says ... when you actually have two or more digits of precision, and you're rounding, you don't round each level of precision. You simply take the single digit (whatever it is) that's one more than your desired precision level, and apply your rounding rules to that!
In the above case, that would be a 4: 14.5548
Therefore, we'd round this 4 down, resulting in 14.55.
Now, if we started with, say, 14.5552, then we have to decide our "rounding rule". The most common are: 1) round the 5 up, or 2) round to the nearest even number (bankers rounding).
Just to give examples of bankers rounding, we do the following: 14.5552 would round to 14.56, and 14.5452 would round to 14.54.
EDIT: Just looking at my examples, I suppose you could derive an enhanced bankers rounding, looking to see if you actually did have more precision. For instance, in my 14.5452 example, it's clear that we're slightly more than .005, so we could round up in that case (somewhat ignoring the "simple" bankers rounding). (That also illustrates why rule #1 has some merit.)
It'd be interesting to see exactly how VB6 does it with various functions, but I'll leave that for others.
Last edited by Elroy; Sep 10th, 2021 at 11:28 AM.
Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will reattach those licenses and/or attributions. To all, peace and happiness.

Sep 10th, 2021, 12:41 PM
#5
Thread Starter
New Member
Re: Rounding a 4 decimal place number to 2 decimal places
Thanks for your replies.
I always thought that rounding started with the furthest digit then work left, so 14.5548 becomes 14.555, which becomes 14.556, which becomes 14.56. I use Sage 50 accounts software and it seems to be doing it this way when calculating settlement discounts on invoices. My VB6 app integrates with Sage and I need it to calculate and produce the same values as Sage. The round(round(amt,3),2) achieves this, so I'm going to have to go with it.

Sep 10th, 2021, 02:07 PM
#6
Re: Rounding a 4 decimal place number to 2 decimal places
This is not a correct approach, you keep rounding and rounding

Sep 10th, 2021, 04:13 PM
#7
Re: Rounding a 4 decimal place number to 2 decimal places
Vb's Round function applies bankers rounding. It has its quirks and is not especially quick. See http://www.xbeat.net/vbspeed/c_Round.htm

Sep 11th, 2021, 07:30 AM
#8
Re: Rounding a 4 decimal place number to 2 decimal places
I have a 4 decimal place number e.g. 14.5548 that I want rounded up correctly to 2 decimal places. So the correct rounded number in this example should be 14.56
NO! See the other threads above. (as "4" is less than 5, 14.55 is the correct 'rounding' result).
What YOU want is to ADD a single digit to your second place decimal "IF ANY NUMBERS FOLLOW IT." At least that is what your example indicates...If so, pretty darn easy...see if there is a third digit, and if so, add one to the 2nd digit.
I'm not a banker, but if I were, and were figuring out how to add interest to someone's account, I'd surely want my result to be 14.55, NOT 14.56. ~smile~
Sam I am (as well as Confused at times).
Tags for this Thread
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
