简体   繁体   English

四舍五入到小数点后两位C#

[英]Rounding to 2 decimal places c#

Currently have a working rounding model within my c# code and is perfectly rounding numbers that have more than 2 decimal places down to 2 decimal places which is great. 目前在我的C#代码中有一个有效的舍入模型,并且可以完美舍入具有2个以上小数位到2个小数位的数字,这很好。 However, when i have lets say double value = 100.6 , and i put that into double dollar_value = Math.Round(value, 2) , it still returns as 100.6. 但是,当我说double value = 100.6 ,并将其放入double dollar_value = Math.Round(value, 2) ,它仍然返回100.6。

I was wondering if there was a way to transform a 1 decimal place value to 2 decimal places? 我想知道是否有办法将1个小数位的值转换为2个小数位?

Numbers are not stored with extra zeroes (As it is a waste of memory to do so, being the numbers are the same with or without). 数字不会以多余的零存储(因为这样做会浪费内存,因为无论有无数字都相同)。 In order to represent a number this way you will either need to display or store it as a string. 为了以这种方式表示数字,您将需要显示或将其存储为字符串。

string str = value.ToString("#.00", CultureInfo.InvariantCulture);

Now str will always have 2 decimal places. 现在str总是有2个小数位。

I don't know the C# method, but in C++ I'd use one of these two methods: 我不知道C#方法,但是在C ++中,我将使用以下两种方法之一:

double value = 23.666666 ; // example
value = 0.01 * floor ( value * 100.0 ) ; // There's a "floor" function in C# too

^ See https://msdn.microsoft.com/en-us/library/e0b5f0xb(v=vs.110).aspx ^参见https://msdn.microsoft.com/zh-cn/library/e0b5f0xb(v=vs.110).aspx

Or 要么

double value = 23.666666 ; // example
value = 0.01 * (double) ( (int)(value*100.0) ) ;

Or 要么

double value = 23.666666 ; // example
value = 0.01 * double ( int ( value*100.0 ) ) ; // same as previous, but more C++ like

The other answers are probably better if you're looking to "print a dollar amount with two decimal places." 如果您要“打印一个小数点后两位的美元金额”,其他答案可能会更好。 However, if you want to transform the number to use internally, this is a way to do it. 但是,如果要转换数字以供内部使用,则可以使用此方法。

If you want the string representation to have two decimal points use: yourNumber.ToString ("0.00"); 如果希望字符串表示形式有两个小数点,请使用: yourNumber.ToString ("0.00");

The number itself is always stored as a ~29 digit number regardless of its string representation. 数字本身始终存储为〜29位数字,无论其字符串表示形式如何。

Your value just needs to be formatted when it's display - for example value.ToString("N2") will convert it to a string with two decimal places. 您的值只需要在显示时进行格式化-例如, value.ToString("N2")会将其转换为带有两位小数的字符串。 Check out the Standard Numeric Format Strings on MSDN to see a broader list of formatting strings. 在MSDN上查看标准数字格式字符串 ,以查看格式字符串的更广泛的列表。

Additionally, I'd only convert to a string when you're ready display the value to a user and would keep it as a numeric type (eg double) if you're passing it around between methods or planning to do any further calculations on it. 另外,我只在准备好向用户显示值时才转换为字符串,并且如果您在方法之间传递或计划进行进一步的计算时将其保留为数字类型(例如,双精度)。它。 Otherwise you'll be unnecessarily converting the value to and from a string multiple times. 否则,您将不必要地多次在字符串之间来回转换值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM