# Thread: Having problem in decimal rounding off

1. ## Having problem in decimal rounding off

I have a function which is doing calculation and then returning a string after rounding off the decimals. Silk is not rounding off in certain cases. I will give those examples in the end.
This the code-

[-] String CalculateCharges(String sCost, String sMargin)
[ ] Real iTemp = 0, iTemp1 = 0
[ ]
[-] if ( sCost != "")
[ ] iTemp = val(sCost)
[-] if ( sMargin != "")
[ ] iTemp1 = val(sMargin)
[ ] iTemp = iTemp + (iTemp * iTemp1)/100
[ ] iVal = iTemp
[ ] REAL i = val(sMargin)
[ ] return Str(iTemp, len(Str(iTemp)),2)
[-] else
[ ] LogWarning("MRC/NRC cost is 0. Please provide the correct data")
[ ] return Str(iTemp)

----------------------------------

The call Str(iTemp, len(Str(iTemp)),2) returns it upto two decimals places and this round off the decimals

Now of the value of iTemp = 9022.695 then it is supposed to return 9022.70 but it is returning 9022.69.

If iTemp = 9022.696 then it correctly rounds of to 9022.70

Is it a flaw in silk test where it starts rounding off if the next digit is greater than 5?

Anybody aware of this? Is there a better solution?

Thanks, Manohar

2. ## Re: Having problem in decimal rounding off

This is a problem with the binary representation of decimals. There is a solution in Segue's knowledge base. Check out Resolution #15649. It offers this piece of alternative code...

<font class="small">Code:</font><hr /><pre>
[-] REAL Round (REAL rOriginal, INTEGER iPlaces)
[ ] REAL rRtn
[ ] INTEGER iMultiplier
[ ]
[ ] // raise 10 to the power of the places desired
[ ] iMultiplier = 10**iPlaces
[ ]
[ ] // multiply by the multiplier and add 0.5
[ ] rRtn = (rOriginal * iMultiplier) + 0.5
[ ]
[ ] // hack off everything after the decimal
[ ] rRtn = [INTEGER]rRtn
[ ]
[ ] // now reduce it
[ ] rRtn = rRtn / iMultiplier
[ ]
[ ] // and return
[ ] return rRtn
</pre><hr />

Rather clever actually.

Brendan

3. ## Re: Having problem in decimal rounding off

Brendan,
Thanks for the peice of code, when i excuted the code the value gets rounded but i also get zero's in the rounded number i.e i get 6543.35000 as rounded number whereas i wanted 6543.35 only, Can you please let me know why this happens and how can i resolve this issue.

Thanks,
Manohar

4. ## Re: Having problem in decimal rounding off

After rounding you can just do Str(myNumber, null, 2) and it will chop off the extra zeros leaving two decimal places.

Brendan

5. ## Re: Having problem in decimal rounding off

this solution actually doesn't work very well.
if the number is greater than Max_INTEGER than you will have a problem.

#### Posting Permissions

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

vBulletin Optimisation provided by vB Optimise v2.6.0 Beta 4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.0.9 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Questions / Answers Form provided by vBAnswers (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
vBNominatevBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Feedback Buttons provided by Advanced Post Thanks / Like (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Username Changing provided by Username Change (Free) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
BetaSoft Inc.
Digital Point modules: Sphinx-based search
All times are GMT -8. The time now is 05:36 AM.