Notifications
Clear all

[Closed] Round a Value

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
 	)
 )
 
6 Replies

You can use “Ceil” and “Floor” to get the nearest whole numbers to a given float.

Hi Marco Brunetta

how say ceil to get value 45.257 and return 50 and not 46.0 ?
and how to say floor to get a value 41.257 and return 40 not 41.0 ?

i tried a lot of methods and here is a fastest and cheapest:


 fn roundFloat d pre:0.1 =
 (
  d = (d as float)/pre
  v = if (d - (v1 = floor d)) > ((v2 = ceil d) - d) then v2 else v1 
  v*pre
 )

Hi Denis :wavey:

with you function I cant get right result

roundFloat 45.258 pre:1
return 45.0 not 50 :-)))

Is not a typical round…
What I want is to round it much more or much less :-))))

–something like this should be done for last value in 4{5}.0
fn roundInteger intg = if intg > 4 then 10 else 0

what is the name of the function? roundFloat

roundFloat 45.0 pre:10
–40.0
roundFloat 45.1 pre:10
–50.0

1 Reply
(@merlinel)
Joined: 11 months ago

Posts: 0

OH my :rolleyes: , RIGHT!!! Im blind

Thank You Denis