如何跳过四舍五入 [英] How to skip rounding off
本文介绍了如何跳过四舍五入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
int NumValue = 123.6;
string StrValue = NumValue.ToString("00000");
分配给StrValue的值是00124而不是00123(表示圆形off)。
请指导我如何跳过四舍五入????
The value assigned to "StrValue" is 00124 instead of 00123 (Means round off).
Please guide me that how to skip rounding off ????
推荐答案
你可以演员 [ ^ ]double
到int
或使用 Math.Truncate [ ^ ]功能。
施放:
You can either cast[^] thedouble
to anint
or use the Math.Truncate[^] function.
Casting:
int NumValue = (int)123.6;
截断:
Truncate:
int NumValue = Math.Truncate( 123.6 );
不要将浮动数分配给int fisrt
Don't assign floating number to int fisrt
float NumValue = 123.6f;
string StrValue = Math.Floor(NumValue).ToString("00000");
嗯...不会编译,因为你会得到一个不能隐式转换输入错误。
如果你使用整数并投出它,你将无法四舍五入:
Well... that won't compile, as you will get a "Cannot implicitly convert type" error.
If you use integers and cast it, you won't get rounded:
int NumValue = (int) 123.6;
string StrValue = NumValue.ToString("00000");
或者,如果您使用的是浮点数,那么您可以明确说出您想要的四舍五入:
Or, if you are using floating point then you can explicitly say what rounding you want:
double NumValue = (int) 123.6;
string StrValue = Math.Round(NumValue, 0).ToString("00000");
这篇关于如何跳过四舍五入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文