Computer Science Canada

Help with displaying real values

Author:  MattyGG [ Tue Feb 07, 2006 2:50 pm ]
Post subject:  Help with displaying real values

I'm trying to display real values as dollar values, but if the user inputs a real value to under 2 decimal places, it will only output up to those two spaces...eg, the user inputs "6.5" it outputs "6.5" when i want it to output "6.50"

Author:  Tony [ Tue Feb 07, 2006 3:04 pm ]
Post subject: 

code:

put real_value:2

where :n will format to n decimal places.

Author:  Delos [ Tue Feb 07, 2006 4:33 pm ]
Post subject: 

Tony wrote:
code:

put real_value:2

where :n will format to n decimal places.


Oh, so close Tony, so very close. The first colon : number complex refers to the number of spaces allocated to that output. For decimal places, you'll want the second colon : number.

i.e.

code:

put real_value : 0 : 2
% 0 means it may use as much space as necessary.

Author:  Cervantes [ Tue Feb 07, 2006 4:43 pm ]
Post subject: 

This can also be done using frealstr which is a type convertion from real to string, where f stands for fraction.

frealstr (r : real, width, fractionWidth : int)

code:

put frealstr (6.5, 0, 2)


It's basically the same as what Delos has, there. The only difference is that this is a manual type convertion, and thus allows you to use it in Font.Draw or other such places where you need a string.


: