1

如何在小数点后打印浮点/双精度变量的数字?例如,435.5644它将输出5644.

4

3 回答 3

4

尝试

fraction = value - (long) value;

或者 :

fraction = value - Math.Floor(value);
于 2012-07-31T15:50:39.150 回答
1

您可以尝试以下方法:

  double d = 435.5644;
  int n = (int)d;

  var v = d - n;

  string s = string.Format("{0:#.0000}", v);

  var result = s.Substring(1);

结果:5644

于 2012-07-31T15:54:19.983 回答
-1

EDIT: reference to another question (http://stackoverflow.com/questions/4512306/get-decimal-part-of-a-number-in-javascript) You can do the following:

double d = 435.5644;
float f = 435.5644f;
Console.WriteLine(Math.Round(d % 1, 4) * 10000);
Console.WriteLine(Math.Round(f % 1, 4) * 10000);

That will give you the integer part you looking for.

Best is to do it as Aghilas Yakoub answered, however, here below another option using string handling. Assuming all amounts will have decimals and that decimal separator is a dot (.) you just need to get the index 1.

double d = 435.5644;
Console.WriteLine(d.ToString().Split('.')[1]);

float f = 435.5644f;
Console.WriteLine(f.ToString().Split('.')[1]);

Otherwise you may get a Unhandled Exception: System.IndexOutOfRangeException.

于 2012-07-31T16:04:01.913 回答