我如何四舍五入小数在净2位小数? [英] How do I round down a decimal to 2 decimal places in .Net?

查看:167
本文介绍了我如何四舍五入小数在净2位小数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该很容易,但我不能找到一个内置的方法吧,.NET框架必须有一个方法来做到这一点!

 私人小数RoundDownTo2DecimalPlaces(小数点输入)
{
   如果(输入℃,)
   {
      抛出新的异常(不negitive号测试);
   }

   //必须有一个更好的办法!
   返回Math.Truncate(输入* 100)/ 100;
}
 

解决方案

没有建立在.NET Framework中做到这一点的方法,其他的答案说怎么写自己的code。

This should be easy, but I can’t find a built in method for it, the .net framework must have a method to do this!

private decimal RoundDownTo2DecimalPlaces(decimal input)
{
   if (input < 0)
   {
      throw new Exception("not tested with negitive numbers");
   }

   // There must be a better way!
   return Math.Truncate(input * 100) / 100;
}

解决方案

There is no build in method in the .net framework to do this, other answers say how to write your own code.

这篇关于我如何四舍五入小数在净2位小数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆