how to round a number to a specific decimal?

   25720   5   2
User Avatar
Member
479 posts
Joined: Dec. 2009
Offline
e.g. I want to round 1.233678 to the third decimal to get 1.234

I've checked the “Expression Functions” and there doesn't seem to be a function to round a number to a specific decimal.

Is there a function to do this?

Thanks!
User Avatar
Member
479 posts
Joined: Dec. 2009
Offline
well, I've figured it out:

round the number to the 3rd decimal by using:
round(1.23456*1000)/1000
you'll get: 1.235

Please correct me if this is not the correct way to do it.
User Avatar
Member
224 posts
Joined: June 2009
Offline
the python round function lets you specify the number of decimal places

http://docs.python.org/library/functions.html#round [docs.python.org]

For example:

round(1.2345678, 2)
Patrick
User Avatar
Staff
2540 posts
Joined: July 2005
Offline
Using hscript wrap your round with padzero(round(<myvalue>), 2)

or use the python round function.
There's at least one school like the old school!
User Avatar
Member
479 posts
Joined: Dec. 2009
Offline
Thank you, Patrick and jeff!

I seem to have problem when using the round function in Python.

I can get 1.23 by using round(1.23456, 2)

But I get 1.2350000000000001 by using round(1.23456, 3)

or get 1.2345999999999999 for round(1.23456, 4) in the Python IDE.

I don't know the details on how the round function performs in Python. The long value i get must be related to how Python recognize float value…

Can you shed light on this issue?

Anyway, thank both of you!

Ji
User Avatar
Member
1 posts
Joined: Nov. 2015
Offline
float

float f = 10.123456F;
float fc = (float)Math.Round(f * 100f) / 100f;

Double

Double d = 100.123456;
Double dc = Math.Round((Double)d, 2);

Decimal

decimal d = 100.123456M;
decimal dc = Math.Round(d, 2);

More about….Math.Round Method [net-informations.com]

Jeryy
  • Quick Links