SRL Archive - Research & Development Lounge

function RoundDec(number: extended; decimals: integer): extended


mixster

2008-03-21 17:40:23

I made this while experimenting with Delphi and changed it to run in Scar. I though it may be useful to someone, and thus it made its way here:
function RoundDec(number: extended; decimals: integer): extended;
begin
Result := Round(number * Pow(10, decimals)) / Pow(10, decimals);
end;

You can test it with this:
program New;

function RoundDec(number: extended; decimals: integer): extended;
begin
Result := Round(number * Pow(10, decimals)) / Pow(10, decimals);
end;

begin
Writeln(FloatToStr(RoundDec(1.23456789, 8)));
end.
And change the 8 to different numbers to see it in action.

Cazax

2008-03-21 17:44:39

Nice function, i was so annoyed when i put:
program New;
begin
Writeln(FloatToStr(7 div 2));
end.

and it says:

Successfully compiled
3
Successfully executed

mixster

2008-03-21 18:07:23

div automatically rounds it down - if you don't want it to do that, then use / with variables or /blah.0 if not (so 7 / 2.0) so it knows to return a float :)
(Wizzup? posted it btw so it must be true)

bullzeye95

2008-03-21 19:30:55

Actually, in scar, div and / are exactly the same. It's only in Delphi that div floors the quotient. To get a float as a result, at least one number must be a float.

Naum

2008-03-22 05:49:34

Just wanted to ask whats a float?

ZephyrsFury

2008-03-22 06:07:04

From: NaumanAkhlaQ
Just wanted to ask whats a float?


A decimal number. Same meaning as an extended.

EDIT: This function should be included in SCAR.

mixster

2008-03-22 08:16:35

Glad you think it should be included in Scar - I just thought it may be useful to someone :)

Freddy1990

2008-03-22 09:00:34

Unfortunately unless you can convince me it's that useful, I don't think it should be included in SCAR…

mixster

2008-03-22 09:09:07

The only time I could think of when you would use it would be for visual effect, but I didn't think it would be added anyway.