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!
how to round a number to a specific decimal?
25720 5 2- oat
- Member
- 479 posts
- Joined: Dec. 2009
- Offline
- oat
- Member
- 479 posts
- Joined: Dec. 2009
- Offline
- phtj
- 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)
http://docs.python.org/library/functions.html#round [docs.python.org]
For example:
round(1.2345678, 2)
Patrick
- old_school
- Staff
- 2540 posts
- Joined: July 2005
- Offline
- oat
- 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
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
- Dell Mercant
- 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
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