如何在c#中舍入十进制值? [英] How to round decimal value in c# ?

查看:84
本文介绍了如何在c#中舍入十进制值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

计算十进制的小值,预期结果是= 345.00但结果得到= 345.0000



我的代码是这个

 < span class =code-keyword> decimal  temp =  6900  *( 5  / < span class =code-digit> 100 ); 





temp = 345.0000如何围绕这个?我想显示345.00?这是我的错吗?我尝试了这段代码

 Math.Round(tempdiscountprice, 2 ); 

也在这里工作。帮帮我

解决方案

数字值没有他们记住的位数。只有当您实际将它们作为字符串输出给用户或文件时才能确切地说明它们的显示方式。

您的代码存在许多问题:

  decimal  temp =  6900  *( 5  /  100 ); 

不是345.0000:它是0.0因为5是一个整数值,因为100和5/100在整数数学中为零。所以你真正需要做的是:

  decimal  temp = 6900M *(5M / 100M); 

表示十进制数字和十进制数学。

在这种情况下,Math.Round将不执行任何操作,因为345的值已经四舍五入到小数点后两位:舍入删除了尾随数字并将数字转换为66.66666到66.67。



要将数字格式化为特定的小数位数,需要将其转换为字符串,并指定数字你想要的数字:

<前lang =cs> 十进制 temp = 6900M *(5M / 100M);
decimal rounded = Math.Round(temp, 2 );
Console.WriteLine( {0:0.00}:{1:0.00},温度,圆润);

哪个会给你:

345.00:345.00

345.00:345.00

或:

<前lang =cs> 十进制 temp = 66 .66666M;
decimal rounded = Math.Round(temp, 2 );
Console.WriteLine( {0}:{1},temp,舍入) ;
Console.WriteLine( {0:0.00}:{1:0.00}, temp,舍入);

它给你:

  66  .66666: 66.  67  
66 .67:66。 67


很可能,你真的不想舍入数值。舍入本身很少使用,甚至可能很危险,因为在计算中舍入一些中间值会导致精度损失。最有可能的是,您只需要在屏幕上以圆形形式显示一个数字。这有点不同:您可以使用字符串格式。请在此处查看所有方法 ToString http://msdn.microsoft.com/en-us/library/system.decimal%28v=vs.110%29.aspx [ ^ ]。



正如您所看到的,数值的格式可以不同的方式完成,具体取决于文化(当前线程文化或通过 IFormatProvider 参数),选项和/或格式字符串。有关格式字符串,请参阅:

http ://msdn.microsoft.com/en-us/library/fzeeb5cd%28v=vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx [ ^ ],

http:/ /msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx [ ^ ]。



-SA


您好,



您可以在下面使用:



  decimal  temp =( decimal )( 6900  00  *( 5  00  /  100  00 )); 
Response.Write( string .Format( {0:### 0.00},temp));


Am calculating small values in decimal, Expected result is = 345.00 but Result getting is = 345.0000

my code is this

decimal temp= 6900 * ( 5 / 100);



temp = 345.0000 how to round this ? I want to show 345.00 ? whats my mistake here ? I tried this code

Math.Round(tempdiscountprice, 2);

also but it wont work here. Help me

解决方案

Numeric values do not have a "number of digits" which they remember. Only when you actually output them as strings to the user or a file can you specific exactly how they are displayed.
There are a number of things wrong with your code:

decimal temp= 6900 * ( 5 / 100);

Is not "345.0000": it is 0.0 because 5 is an integer value, as is 100, and 5 / 100 is zero in integer math. So what you actually need to do is:

decimal temp= 6900M * ( 5M / 100M);

to indicate Decimal numebers and Decimal math.
In this case, Math.Round will do nothing, as the value of 345 is already rounded to 2 decimal places: Rounding removed trailing digits and converts numbers like 66.66666 to 66.67.

To format a number to a specific number of decimal places, you need to convert it to a string, and specify the number of digits you want:

decimal temp = 6900M * (5M / 100M);
decimal rounded = Math.Round(temp, 2);
Console.WriteLine("{0:0.00}:{1:0.00}", temp, rounded);

Which will give you:
345.00:345.00
345.00:345.00
Or:

decimal temp = 66.66666M;
decimal rounded = Math.Round(temp, 2);
Console.WriteLine("{0}:{1}", temp, rounded);
Console.WriteLine("{0:0.00}:{1:0.00}", temp, rounded);

Which gives you:

66.66666:66.67
66.67:66.67


Most likely, you don't really want to round a numeric value. Rounding per se is pretty rarely used and can even be dangerous, because rounding of some intermediate values in calculations can cause precision loss. Most likely, you only need to show a number on screen in rounded form. This is something a bit different: you can use string formatting instead. Please see all the methods ToString here: http://msdn.microsoft.com/en-us/library/system.decimal%28v=vs.110%29.aspx[^].

As you can see, the format of the numeric value can be done differently, depending on culture (current thread culture or explicitly specified via the IFormatProvider parameter), options and/or format string. For format strings, please see:
http://msdn.microsoft.com/en-us/library/fzeeb5cd%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx[^].

—SA


Hi,

You can use below:

decimal temp = (decimal)(6900.00 * (5.00 / 100.00));
Response.Write(string.Format("{0:###0.00}", temp));


这篇关于如何在c#中舍入十进制值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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