Computer Science Canada

Rounding

Author:  ElimPi [ Mon Jun 19, 2006 4:02 pm ]
Post subject:  Rounding

I have a problem with rounding real numbers. I need to be able to round them to two decimal places without using field width specifiers. The reason is that I'm making a shop, and after tax is calculated, I get a real number to an obscenely long number of decimal places. I need to output the number as a Font.Draw, I know how to use realstr, but it puts it to a varying number of decimal places depending on how many digits precede the decimal. Any ideas?

Author:  [Gandalf] [ Mon Jun 19, 2006 5:00 pm ]
Post subject: 

Look up the frealstr function. It looks like this:
frealstr ( r : real, width, fractionWidth : int ) : string
code:
put frealstr (1.657, 0, 1) % round 1.657 to one decimal place (1.7)

Author:  aldreneo [ Mon Jun 19, 2006 8:58 pm ]
Post subject: 

Dude I was about to ask how to do rounding, thanks

Author:  [Gandalf] [ Wed Jun 21, 2006 8:36 am ]
Post subject: 

No problem. Maybe it's a sign of Delos' clairvoyance spreading. Wink
Note that this function is only needed when you need to round to a specific amount of decimal places. If you only need to round a real number to an integer then you can use round (myNum : real) : int.

Author:  Delos [ Wed Jun 21, 2006 9:19 am ]
Post subject: 

[Gandalf] wrote:
No problem. Maybe it's a sign of Delos' clairvoyance spreading. Wink


About time too! What's the point of answering questions if you can't pre-empt other question's answers in the process? Wink


: