Possible Duplicate:
c# - How do I round a decimal value to 2 decimal places (for output on a page)可能重复:c# - 如何将十进制值舍入到2个小数位(用于页面上的输出)
string strTemp = "0.51667308807373";
convert to decimal by rounding of two decimal places.
通过舍入两位小数来转换为十进制。
6 个解决方案
#1
36
Math.Round(Convert.ToDecimal(strTemp), 2);
#2
14
First convert string to decimal (Using Decimal.Parse or Decimal.TryParse).
首先将字符串转换为十进制(使用Decimal.Parse或Decimal.TryParse)。
decimal d = Decimal.Parse("123.45678");
Then round the decimal value using Round(d, m) where d is your number, m is the number of decimals, see http://msdn.microsoft.com/en-us/library/6be1edhb.aspx
然后使用Round(d,m)舍入十进制值,其中d是您的数字,m是小数位数,请参阅http://msdn.microsoft.com/en-us/library/6be1edhb.aspx
decimal rounded = Decimal.Round(d, 2);
If you only want to round for presentation, you can skip rounding to a decimal and instead simply round the value in output:
如果您只想舍入演示文稿,则可以跳过舍入到小数而不是简单地舍入输出中的值:
String.Format("{0:0.00}", 123.45678m);
#3
4
Convert the value to a floating point number, then round it:
将值转换为浮点数,然后将其舍入:
double temp = Double.Parse(strTemp, CultureInfo.InvariantCulture);
temp = Math.Round(temp, 2);
Alternatively, if you want the result as a string, just parse it and format it to two decimal places:
或者,如果您希望将结果作为字符串,只需解析它并将其格式化为两个小数位:
double temp = Double.Parse(strTemp, CultureInfo.InvariantCulture);
string result = temp.ToString("N2", CultureInfo.InvariantCulture);
Note: The CultureInfo
object is so that the methods will always use a period as decimal separator, regardless of the local culture settings.
注意:CultureInfo对象使得方法将始终使用句点作为小数分隔符,而不管本地区域性设置如何。
#4
0
var roundedTemp = Math.Round(decimal.Parse(strTemp), 2);
You might want to check to ensure the string is always a decimal first but think this is the essence of it.
您可能需要检查以确保字符串始终是小数,但认为这是它的本质。
#5
0
you can use info from this link http://www.csharp-examples.net/string-format-double/ for the double value, use double.parse api
您可以使用此链接中的信息http://www.csharp-examples.net/string-format-double/获取double值,使用double.parse api
#6
0
You can use Number format Info. Something like
您可以使用数字格式信息。就像是
NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;
double myInt = 0.51667308807373;
// Displays the same value with four decimal digits.
nfi.NumberDecimalDigits = 2;
Console.WriteLine(myInt.ToString("N", nfi));
Console.ReadKey();
#1
36
Math.Round(Convert.ToDecimal(strTemp), 2);
#2
14
First convert string to decimal (Using Decimal.Parse or Decimal.TryParse).
首先将字符串转换为十进制(使用Decimal.Parse或Decimal.TryParse)。
decimal d = Decimal.Parse("123.45678");
Then round the decimal value using Round(d, m) where d is your number, m is the number of decimals, see http://msdn.microsoft.com/en-us/library/6be1edhb.aspx
然后使用Round(d,m)舍入十进制值,其中d是您的数字,m是小数位数,请参阅http://msdn.microsoft.com/en-us/library/6be1edhb.aspx
decimal rounded = Decimal.Round(d, 2);
If you only want to round for presentation, you can skip rounding to a decimal and instead simply round the value in output:
如果您只想舍入演示文稿,则可以跳过舍入到小数而不是简单地舍入输出中的值:
String.Format("{0:0.00}", 123.45678m);
#3
4
Convert the value to a floating point number, then round it:
将值转换为浮点数,然后将其舍入:
double temp = Double.Parse(strTemp, CultureInfo.InvariantCulture);
temp = Math.Round(temp, 2);
Alternatively, if you want the result as a string, just parse it and format it to two decimal places:
或者,如果您希望将结果作为字符串,只需解析它并将其格式化为两个小数位:
double temp = Double.Parse(strTemp, CultureInfo.InvariantCulture);
string result = temp.ToString("N2", CultureInfo.InvariantCulture);
Note: The CultureInfo
object is so that the methods will always use a period as decimal separator, regardless of the local culture settings.
注意:CultureInfo对象使得方法将始终使用句点作为小数分隔符,而不管本地区域性设置如何。
#4
0
var roundedTemp = Math.Round(decimal.Parse(strTemp), 2);
You might want to check to ensure the string is always a decimal first but think this is the essence of it.
您可能需要检查以确保字符串始终是小数,但认为这是它的本质。
#5
0
you can use info from this link http://www.csharp-examples.net/string-format-double/ for the double value, use double.parse api
您可以使用此链接中的信息http://www.csharp-examples.net/string-format-double/获取double值,使用double.parse api
#6
0
You can use Number format Info. Something like
您可以使用数字格式信息。就像是
NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;
double myInt = 0.51667308807373;
// Displays the same value with four decimal digits.
nfi.NumberDecimalDigits = 2;
Console.WriteLine(myInt.ToString("N", nfi));
Console.ReadKey();