# Thread: Rounding a 4 decimal place number to 2 decimal places

1. ## 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)  Reply With Quote

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.  Reply With Quote

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.  Reply With Quote

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.  Reply With Quote

5. ## Re: Rounding a 4 decimal place number to 2 decimal places

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.  Reply With Quote

6. ## Re: Rounding a 4 decimal place number to 2 decimal places

This is not a correct approach, you keep rounding and rounding  Reply With Quote

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  Reply With Quote

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~  Reply With Quote

round format decimal #### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•