 
Junior Member
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 
Senior Member
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 
Junior Member
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 
Senior Member
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 
Junior Member
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

Forum Rules 