MerlinEl
11-02-2009, 08:34 PM
How to round float numbers to get this result?
0.45 == 0
0.85 == 0
or
4.25 == 0
6.25 == 10
or
45.5 == 50
44.5 == 40
--this will do it , but is to slow
fn roundLastInt val =
(
fn roundInteger intg = if intg > 4 then 10 else 0
val = val as integer -- cut float values if exist
str = val as string
if str.count == 1 then -- if value si between 0 to 9
(
return roundInteger val
)
else if str.count > 1 do -- if value is between 10 to infinite
(
new_str = substring str 1 (str.count-1) --cut the last int
last_int = roundInteger (str[str.count] as integer) --round last int
return ((new_str+"0") as integer + last_int) as integer -- join last int back
)
)
0.45 == 0
0.85 == 0
or
4.25 == 0
6.25 == 10
or
45.5 == 50
44.5 == 40
--this will do it , but is to slow
fn roundLastInt val =
(
fn roundInteger intg = if intg > 4 then 10 else 0
val = val as integer -- cut float values if exist
str = val as string
if str.count == 1 then -- if value si between 0 to 9
(
return roundInteger val
)
else if str.count > 1 do -- if value is between 10 to infinite
(
new_str = substring str 1 (str.count-1) --cut the last int
last_int = roundInteger (str[str.count] as integer) --round last int
return ((new_str+"0") as integer + last_int) as integer -- join last int back
)
)
