我如何四舍五入到最接近的整数
How do i round to the nearest whole number
我在可为 null 的 DateTime 上调用 ToMinutes(),此 returns 为双精度但我希望将其转换为 最接近的整数 如果它不为空。
示例:从 1534488.74496255
到 1534489
我该怎么做?
会不会是你要找的像下面这样的东西?这个 returns 最接近的小整数。
int timeAsWholeMinute = (int) Math.Floor(timeInMinutes);
或者如果您只想要最接近的整数:
int timeAsWholeMinute = (int) Math.Round(timeInMinutes);
想四舍五入就四舍五入:
double source = 1534488.74496255;
// if you want double (i.e. floating point result)
double result = Math.Round(source);
// if you want integer outcome (and source is positive)
int minutes = (int) (source + 0.5);
// if you want integer outcome (general case)
int minutes = (int) (source > 0 ? source + 0.5 : source - 0.5);
我在可为 null 的 DateTime 上调用 ToMinutes(),此 returns 为双精度但我希望将其转换为 最接近的整数 如果它不为空。
示例:从 1534488.74496255
到 1534489
我该怎么做?
会不会是你要找的像下面这样的东西?这个 returns 最接近的小整数。
int timeAsWholeMinute = (int) Math.Floor(timeInMinutes);
或者如果您只想要最接近的整数:
int timeAsWholeMinute = (int) Math.Round(timeInMinutes);
想四舍五入就四舍五入:
double source = 1534488.74496255;
// if you want double (i.e. floating point result)
double result = Math.Round(source);
// if you want integer outcome (and source is positive)
int minutes = (int) (source + 0.5);
// if you want integer outcome (general case)
int minutes = (int) (source > 0 ? source + 0.5 : source - 0.5);