C#中如何将数字四舍五入到小数点后两位?

发布于 2024-07-08 10:31:41 字数 31 浏览 9 评论 0原文

我想使用 Math.Round 函数来完成此操作

I want to do this using the Math.Round function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

醉南桥 2024-07-15 10:31:42

字符串a =“10.65678”;

小数 d = Math.Round(Convert.ToDouble(a.ToString()),2)

string a = "10.65678";

decimal d = Math.Round(Convert.ToDouble(a.ToString()),2)

初熏 2024-07-15 10:31:42
  public double RoundDown(double number, int decimalPlaces)
        {
            return Math.Floor(number * Math.Pow(10, decimalPlaces)) / Math.Pow(10, decimalPlaces);
        }
  public double RoundDown(double number, int decimalPlaces)
        {
            return Math.Floor(number * Math.Pow(10, decimalPlaces)) / Math.Pow(10, decimalPlaces);
        }
苍景流年 2024-07-15 10:31:42

这是一个老问题,但如果您使用 C# 6 或更高版本并希望将数字显示为小数点后 2 位,您可以使用插值字符串并在带冒号的表达式后面指定格式字符串:

var myStringNumber = $"{Math .Round(myDoubleNumber, 2):0.00}";

It's an old question but if you're using C# 6 or above and want to display the number to 2 decimal places, you can you use interpolated strings and specify a format string after an expression with a colon :

var myStringNumber = $"{Math.Round(myDoubleNumber, 2):0.00}";

知足的幸福 2024-07-15 10:31:41

以下是一些示例:

decimal a = 1.994444M;

Math.Round(a, 2); //returns 1.99

decimal b = 1.995555M;

Math.Round(b, 2); //returns 2.00

您可能还想查看具有以下重载的银行家四舍五入/四舍五入:

Math.Round(a, 2, MidpointRounding.ToEven);

有更多信息此处

Here's some examples:

decimal a = 1.994444M;

Math.Round(a, 2); //returns 1.99

decimal b = 1.995555M;

Math.Round(b, 2); //returns 2.00

You might also want to look at bankers rounding / round-to-even with the following overload:

Math.Round(a, 2, MidpointRounding.ToEven);

There's more information on it here.

热鲨 2024-07-15 10:31:41

尝试这个:

twoDec = Math.Round(val, 2)

Try this:

twoDec = Math.Round(val, 2)
独孤求败 2024-07-15 10:31:41

如果您想要一个字符串

> (1.7289).ToString("#.##")
"1.73"

或小数

> Math.Round((Decimal)x, 2)
1.73m

但请记住! 舍入不具有分配性,即。 round(x*y) != round(x) * round(y)。 因此,在计算结束之前不要进行任何舍入,否则您将失去准确性。

If you'd like a string

> (1.7289).ToString("#.##")
"1.73"

Or a decimal

> Math.Round((Decimal)x, 2)
1.73m

But remember! Rounding is not distributive, ie. round(x*y) != round(x) * round(y). So don't do any rounding until the very end of a calculation, else you'll lose accuracy.

久随 2024-07-15 10:31:41

就我个人而言,我从不舍弃任何东西。 尽可能保持坚定,因为无论如何,四舍五入在 CS 中有点转移注意力。 但您确实希望为用户设置数据格式,为此,我发现 string.Format("{0:0.00}", number) 是一个很好的方法。

Personally I never round anything. Keep it as resolute as possible, since rounding is a bit of a red herring in CS anyway. But you do want to format data for your users, and to that end, I find that string.Format("{0:0.00}", number) is a good approach.

冷了相思 2024-07-15 10:31:41

维基百科有一个关于一般舍入的很好的页面

所有 .NET(托管)语言都可以使用任何公共语言运行时 (CLR) 舍入机制。 例如,Math.Round() (如上所述)方法允许开发人员指定舍入类型(舍入到偶数或远离零)。 Convert.ToInt32() 方法及其变体使用 round-to-even< /a>. Ceiling()Floor() 方法是相关的。

您也可以使用自定义数字格式进行舍入。

请注意 Decimal.Round() 使用与 Math.Round() 不同的方法;

这是关于银行家的有用的帖子舍入算法。
请参阅 Raymond 此处的一篇关于舍入的幽默帖子。 。

Wikipedia has a nice page on rounding in general.

All .NET (managed) languages can use any of the common language run time's (the CLR) rounding mechanisms. For example, the Math.Round() (as mentioned above) method allows the developer to specify the type of rounding (Round-to-even or Away-from-zero). The Convert.ToInt32() method and its variations use round-to-even. The Ceiling() and Floor() methods are related.

You can round with custom numeric formatting as well.

Note that Decimal.Round() uses a different method than Math.Round();

Here is a useful post on the banker's rounding algorithm.
See one of Raymond's humorous posts here about rounding...

完美的未来在梦里 2024-07-15 10:31:41

// 最多转换两位小数

String.Format("{0:0.00}", 140.6767554);        // "140.67"
String.Format("{0:0.00}", 140.1);             // "140.10"
String.Format("{0:0.00}", 140);              // "140.00"

Double d = 140.6767554;
Double dc = Math.Round((Double)d, 2);       //  140.67

decimal d = 140.6767554M;
decimal dc = Math.Round(d, 2);             //  140.67

=========

// just two decimal places
String.Format("{0:0.##}", 123.4567);      // "123.46"
String.Format("{0:0.##}", 123.4);         // "123.4"
String.Format("{0:0.##}", 123.0);         // "123"

也可以将“0”与“#”组合起来。

String.Format("{0:0.0#}", 123.4567)       // "123.46"
String.Format("{0:0.0#}", 123.4)          // "123.4"
String.Format("{0:0.0#}", 123.0)          // "123.0"

// convert upto two decimal places

String.Format("{0:0.00}", 140.6767554);        // "140.67"
String.Format("{0:0.00}", 140.1);             // "140.10"
String.Format("{0:0.00}", 140);              // "140.00"

Double d = 140.6767554;
Double dc = Math.Round((Double)d, 2);       //  140.67

decimal d = 140.6767554M;
decimal dc = Math.Round(d, 2);             //  140.67

=========

// just two decimal places
String.Format("{0:0.##}", 123.4567);      // "123.46"
String.Format("{0:0.##}", 123.4);         // "123.4"
String.Format("{0:0.##}", 123.0);         // "123"

can also combine "0" with "#".

String.Format("{0:0.0#}", 123.4567)       // "123.46"
String.Format("{0:0.0#}", 123.4)          // "123.4"
String.Format("{0:0.0#}", 123.0)          // "123.0"
孤芳又自赏 2024-07-15 10:31:41

如果要对数字进行舍入,您可以获得不同的结果,具体取决于: 如何使用 Math.Round() 函数(如果用于向上舍入或向下舍入)、您正在使用双精度数和/或浮点数,然后应用中点舍入。 特别是,当与其中的操作一起使用或要舍入的变量来自操作时。 假设您想要将这两个数字相乘:0.75 * 0.95 = 0.7125。 正确的? 不在 C# 中

让我们看看如果您想四舍五入到小数点后第三位会发生什么:

double result = 0.75d * 0.95d; // result = 0.71249999999999991
double result = 0.75f * 0.95f; // result = 0.71249997615814209

result = Math.Round(result, 3, MidpointRounding.ToEven); // result = 0.712. Ok
result = Math.Round(result, 3, MidpointRounding.AwayFromZero); // result = 0.712. Should be 0.713

如您所见,如果您想向下舍入中点,第一个 Round() 是正确的。 但是第二个 Round() 如果你想向上取整,那就错了。

这适用于负数:

double result = -0.75 * 0.95;  //result = -0.71249999999999991
result = Math.Round(result, 3, MidpointRounding.ToEven); // result = -0.712. Ok
result = Math.Round(result, 3, MidpointRounding.AwayFromZero); // result = -0.712. Should be -0.713

因此,恕我直言,您应该为满足您的要求的 Math.Round() 创建自己的包装函数。 我创建了一个函数,其中参数“roundUp=true”表示四舍五入到下一个更大的数字。 即:0.7125 舍入为 0.713,-0.7125 舍入为 -0.712(因为 -0.712 > -0.713)。 这是我创建的函数,适用于任意位数的小数:

double Redondea(double value, int precision, bool roundUp = true)
{
    if ((decimal)value == 0.0m)
        return 0.0;

    double corrector = 1 / Math.Pow(10, precision + 2);

    if ((decimal)value < 0.0m)
    {
        if (roundUp)
            return Math.Round(value, precision, MidpointRounding.ToEven);
        else
            return Math.Round(value - corrector, precision, MidpointRounding.AwayFromZero);
    }
    else
    {
        if (roundUp)
            return Math.Round(value + corrector, precision, MidpointRounding.AwayFromZero);
        else
            return Math.Round(value, precision, MidpointRounding.ToEven);
    }
}

变量“校正器”用于修复浮点数或双精度数运算的不准确性。

If you want to round a number, you can obtain different results depending on: how you use the Math.Round() function (if for a round-up or round-down), you're working with doubles and/or floats numbers, and you apply the midpoint rounding. Especially, when using with operations inside of it or the variable to round comes from an operation. Let's say, you want to multiply these two numbers: 0.75 * 0.95 = 0.7125. Right? Not in C#

Let's see what happens if you want to round to the 3rd decimal:

double result = 0.75d * 0.95d; // result = 0.71249999999999991
double result = 0.75f * 0.95f; // result = 0.71249997615814209

result = Math.Round(result, 3, MidpointRounding.ToEven); // result = 0.712. Ok
result = Math.Round(result, 3, MidpointRounding.AwayFromZero); // result = 0.712. Should be 0.713

As you see, the first Round() is correct if you want to round down the midpoint. But the second Round() it's wrong if you want to round up.

This applies to negative numbers:

double result = -0.75 * 0.95;  //result = -0.71249999999999991
result = Math.Round(result, 3, MidpointRounding.ToEven); // result = -0.712. Ok
result = Math.Round(result, 3, MidpointRounding.AwayFromZero); // result = -0.712. Should be -0.713

So, IMHO, you should create your own wrap function for Math.Round() that fit your requirements. I created a function in which, the parameter 'roundUp=true' means to round to next greater number. That is: 0.7125 rounds to 0.713 and -0.7125 rounds to -0.712 (because -0.712 > -0.713). This is the function I created and works for any number of decimals:

double Redondea(double value, int precision, bool roundUp = true)
{
    if ((decimal)value == 0.0m)
        return 0.0;

    double corrector = 1 / Math.Pow(10, precision + 2);

    if ((decimal)value < 0.0m)
    {
        if (roundUp)
            return Math.Round(value, precision, MidpointRounding.ToEven);
        else
            return Math.Round(value - corrector, precision, MidpointRounding.AwayFromZero);
    }
    else
    {
        if (roundUp)
            return Math.Round(value + corrector, precision, MidpointRounding.AwayFromZero);
        else
            return Math.Round(value, precision, MidpointRounding.ToEven);
    }
}

The variable 'corrector' is for fixing the inaccuracy of operating with floating or double numbers.

陌路黄昏 2024-07-15 10:31:41

这是为了在 C# 中四舍五入到小数点后两位:

label8.Text = valor_cuota .ToString("N2") ;

在 VB.NET 中:

 Imports System.Math
 round(label8.text,2)

This is for rounding to 2 decimal places in C#:

label8.Text = valor_cuota .ToString("N2") ;

In VB.NET:

 Imports System.Math
 round(label8.text,2)
兰花执着 2024-07-15 10:31:41

我知道这是一个老问题,但请注意数学回合字符串格式回合之间的以下差异:

decimal d1 = (decimal)1.125;
Math.Round(d1, 2).Dump();   // returns 1.12
d1.ToString("#.##").Dump(); // returns "1.13"

decimal d2 = (decimal)1.1251;
Math.Round(d2, 2).Dump();   // returns 1.13
d2.ToString("#.##").Dump(); // returns "1.13"

I know its an old question but please note for the following differences between Math round and String format round:

decimal d1 = (decimal)1.125;
Math.Round(d1, 2).Dump();   // returns 1.12
d1.ToString("#.##").Dump(); // returns "1.13"

decimal d2 = (decimal)1.1251;
Math.Round(d2, 2).Dump();   // returns 1.13
d2.ToString("#.##").Dump(); // returns "1.13"
萌梦深 2024-07-15 10:31:41

有一个奇怪的情况,我有一个十进制变量,当序列化 55.50 时,它总是将数学上的默认值设置为 55.5。 但是,由于某种原因,我们的客户端系统认真地期望 55.50,并且他们肯定期望小数。 就在那时,我编写了下面的帮助程序,它总是将任何用零填充的十进制值转换为 2 位数字,而不是发送字符串。

public static class DecimalExtensions
{
    public static decimal WithTwoDecimalPoints(this decimal val)
    {
        return decimal.Parse(val.ToString("0.00"));
    }
}

用法应该是

var sampleDecimalValueV1 = 2.5m;
Console.WriteLine(sampleDecimalValueV1.WithTwoDecimalPoints());

decimal sampleDecimalValueV1 = 2;
Console.WriteLine(sampleDecimalValueV1.WithTwoDecimalPoints());

输出:

2.50
2.00

Had a weird situation where I had a decimal variable, when serializing 55.50 it always sets default value mathematically as 55.5. But whereas, our client system is seriously expecting 55.50 for some reason and they definitely expected decimal. Thats when I had write the below helper, which always converts any decimal value padded to 2 digits with zeros instead of sending a string.

public static class DecimalExtensions
{
    public static decimal WithTwoDecimalPoints(this decimal val)
    {
        return decimal.Parse(val.ToString("0.00"));
    }
}

Usage should be

var sampleDecimalValueV1 = 2.5m;
Console.WriteLine(sampleDecimalValueV1.WithTwoDecimalPoints());

decimal sampleDecimalValueV1 = 2;
Console.WriteLine(sampleDecimalValueV1.WithTwoDecimalPoints());

Output:

2.50
2.00
葮薆情 2024-07-15 10:31:41

只需 Math.Round() 即可解决此问题,并通过向该方法传递 precision 参数和 MidpointRounding 参数来适应情况。

默认情况下,Math.Round 使用 MidpointRounding.ToEven,这实际上是银行家舍入,意味着中间的数字舍入到最接近的偶数:

1.5 => 2
2.5 => 2
3.5 => 4
4.5 => 4

让我做一下很清楚。 实际上,您可以采用 5 种 MidpointRounding 策略来获得 Math.Round() 的预期结果:

  1. ToEven(默认) 当一个数字介于另外两个数字之间时,它会向最接近的偶数舍入。

  2. AwayFromZero(数学舍入) 当一个数字介于其他两个数字之间时,它会向远离零的最接近的数字舍入。 (又名,四舍五入)

  3. ToZero 根据指定精度对数字进行向下舍入

  4. ToNegativeInfinity (floor) 根据指定精度对数字进行向下舍入

  5. ToPositiveInfinity(上限) 根据指定精度对数字进行向上舍入。

下面是查看它们如何工作的代码:

var num1 = 1.5;
var num2 = 2.5;
var num3 = 3.5;
var num4 = 2.51;
var num5 = 2.48;

Console.WriteLine("----------------Round with MidpointRounding.ToEven (default) ---------------------");
Console.WriteLine($"{num1} => {Math.Round(num1, MidpointRounding.ToEven)}");
Console.WriteLine($"{num2} => {Math.Round(num2, MidpointRounding.ToEven)}");
Console.WriteLine($"{num3} => {Math.Round(num3, MidpointRounding.ToEven)}");
Console.WriteLine($"{num4} => {Math.Round(num4, MidpointRounding.ToEven)}");
Console.WriteLine($"{num5} => {Math.Round(num5, MidpointRounding.ToEven)}");

Console.WriteLine("----------------Round with MidpointRounding.AwayFromZero (mathematical rounding) ---------------------");
Console.WriteLine($"{num1} => {Math.Round(num1, MidpointRounding.AwayFromZero)}");
Console.WriteLine($"{num2} => {Math.Round(num2, MidpointRounding.AwayFromZero)}");
Console.WriteLine($"{num3} => {Math.Round(num3, MidpointRounding.AwayFromZero)}");
Console.WriteLine($"{num4} => {Math.Round(num4, MidpointRounding.AwayFromZero)}");
Console.WriteLine($"{num5} => {Math.Round(num5, MidpointRounding.AwayFromZero)}");

Console.WriteLine("----------------Round with MidpointRounding.ToZero ---------------------");
Console.WriteLine($"{num1} => {Math.Round(num1, MidpointRounding.ToZero)}");
Console.WriteLine($"{num2} => {Math.Round(num2, MidpointRounding.ToZero)}");
Console.WriteLine($"{num3} => {Math.Round(num3, MidpointRounding.ToZero)}");
Console.WriteLine($"{num4} => {Math.Round(num4, MidpointRounding.ToZero)}");
Console.WriteLine($"{num5} => {Math.Round(num5, MidpointRounding.ToZero)}");

Console.WriteLine("----------------Round with MidpointRounding.ToNegativeInfinity (floor) ---------------------");
Console.WriteLine($"{num1} => {Math.Round(num1, MidpointRounding.ToNegativeInfinity)}");
Console.WriteLine($"{num2} => {Math.Round(num2, MidpointRounding.ToNegativeInfinity)}");
Console.WriteLine($"{num3} => {Math.Round(num3, MidpointRounding.ToNegativeInfinity)}");
Console.WriteLine($"{num4} => {Math.Round(num4, MidpointRounding.ToNegativeInfinity)}");
Console.WriteLine($"{num5} => {Math.Round(num5, MidpointRounding.ToNegativeInfinity)}");

Console.WriteLine("----------------Round with MidpointRounding.ToPositiveInfinity (ceiling) ---------------------");
Console.WriteLine($"{num1} => {Math.Round(num1, MidpointRounding.ToPositiveInfinity)}");
Console.WriteLine($"{num2} => {Math.Round(num2, MidpointRounding.ToPositiveInfinity)}");
Console.WriteLine($"{num3} => {Math.Round(num3, MidpointRounding.ToPositiveInfinity)}");
Console.WriteLine($"{num4} => {Math.Round(num4, MidpointRounding.ToPositiveInfinity)}");
Console.WriteLine($"{num5} => {Math.Round(num5, MidpointRounding.ToPositiveInfinity)}");

结果:

----------------Round with MidpointRounding.ToEven (default) ---------------------
1.5 => 2
2.5 => 2
3.5 => 4
2.51 => 3
2.48 => 2
----------------Round with MidpointRounding.AwayFromZero (mathematical rounding) ---------------------
1.5 => 2
2.5 => 3
3.5 => 4
2.51 => 3
2.48 => 2
----------------Round with MidpointRounding.ToZero ---------------------
1.5 => 1
2.5 => 2
3.5 => 3
2.51 => 2
2.48 => 2
----------------Round with MidpointRounding.ToNegativeInfinity ---------------------
1.5 => 1
2.5 => 2
3.5 => 3
2.51 => 2
2.48 => 2
----------------Round with MidpointRounding.ToPositiveInfinity ---------------------
1.5 => 2
2.5 => 3
3.5 => 4
2.51 => 3
2.48 => 3

Simply Math.Round() solves this problem and fits the situation by passing a precision parameter and MidpointRounding parameter to the method.

By default Math.Round uses MidpointRounding.ToEven which is actually bankers rounding meaning that the halfway numbers round to the nearest even number:

1.5 => 2
2.5 => 2
3.5 => 4
4.5 => 4

Let me make it clear. Actually, there are 5 strategies of MidpointRounding you could take to get what you expect from Math.Round():

  1. ToEven (default) When a number is halfway between two others, it is rounded toward the nearest even number.

  2. AwayFromZero (mathematical rounding) When a number is halfway between two others, it is rounded toward the nearest number away from zero. (Aka, round up)

  3. ToZero rounds down the number based on specified precision

  4. ToNegativeInfinity (floor) rounds down the number based on specified precision

  5. ToPositiveInfinity (ceiling) rounds up the number based on specified precision.

Here is the code to see exactly how they work:

var num1 = 1.5;
var num2 = 2.5;
var num3 = 3.5;
var num4 = 2.51;
var num5 = 2.48;

Console.WriteLine("----------------Round with MidpointRounding.ToEven (default) ---------------------");
Console.WriteLine(
quot;{num1} => {Math.Round(num1, MidpointRounding.ToEven)}");
Console.WriteLine(
quot;{num2} => {Math.Round(num2, MidpointRounding.ToEven)}");
Console.WriteLine(
quot;{num3} => {Math.Round(num3, MidpointRounding.ToEven)}");
Console.WriteLine(
quot;{num4} => {Math.Round(num4, MidpointRounding.ToEven)}");
Console.WriteLine(
quot;{num5} => {Math.Round(num5, MidpointRounding.ToEven)}");

Console.WriteLine("----------------Round with MidpointRounding.AwayFromZero (mathematical rounding) ---------------------");
Console.WriteLine(
quot;{num1} => {Math.Round(num1, MidpointRounding.AwayFromZero)}");
Console.WriteLine(
quot;{num2} => {Math.Round(num2, MidpointRounding.AwayFromZero)}");
Console.WriteLine(
quot;{num3} => {Math.Round(num3, MidpointRounding.AwayFromZero)}");
Console.WriteLine(
quot;{num4} => {Math.Round(num4, MidpointRounding.AwayFromZero)}");
Console.WriteLine(
quot;{num5} => {Math.Round(num5, MidpointRounding.AwayFromZero)}");

Console.WriteLine("----------------Round with MidpointRounding.ToZero ---------------------");
Console.WriteLine(
quot;{num1} => {Math.Round(num1, MidpointRounding.ToZero)}");
Console.WriteLine(
quot;{num2} => {Math.Round(num2, MidpointRounding.ToZero)}");
Console.WriteLine(
quot;{num3} => {Math.Round(num3, MidpointRounding.ToZero)}");
Console.WriteLine(
quot;{num4} => {Math.Round(num4, MidpointRounding.ToZero)}");
Console.WriteLine(
quot;{num5} => {Math.Round(num5, MidpointRounding.ToZero)}");

Console.WriteLine("----------------Round with MidpointRounding.ToNegativeInfinity (floor) ---------------------");
Console.WriteLine(
quot;{num1} => {Math.Round(num1, MidpointRounding.ToNegativeInfinity)}");
Console.WriteLine(
quot;{num2} => {Math.Round(num2, MidpointRounding.ToNegativeInfinity)}");
Console.WriteLine(
quot;{num3} => {Math.Round(num3, MidpointRounding.ToNegativeInfinity)}");
Console.WriteLine(
quot;{num4} => {Math.Round(num4, MidpointRounding.ToNegativeInfinity)}");
Console.WriteLine(
quot;{num5} => {Math.Round(num5, MidpointRounding.ToNegativeInfinity)}");

Console.WriteLine("----------------Round with MidpointRounding.ToPositiveInfinity (ceiling) ---------------------");
Console.WriteLine(
quot;{num1} => {Math.Round(num1, MidpointRounding.ToPositiveInfinity)}");
Console.WriteLine(
quot;{num2} => {Math.Round(num2, MidpointRounding.ToPositiveInfinity)}");
Console.WriteLine(
quot;{num3} => {Math.Round(num3, MidpointRounding.ToPositiveInfinity)}");
Console.WriteLine(
quot;{num4} => {Math.Round(num4, MidpointRounding.ToPositiveInfinity)}");
Console.WriteLine(
quot;{num5} => {Math.Round(num5, MidpointRounding.ToPositiveInfinity)}");

the result:

----------------Round with MidpointRounding.ToEven (default) ---------------------
1.5 => 2
2.5 => 2
3.5 => 4
2.51 => 3
2.48 => 2
----------------Round with MidpointRounding.AwayFromZero (mathematical rounding) ---------------------
1.5 => 2
2.5 => 3
3.5 => 4
2.51 => 3
2.48 => 2
----------------Round with MidpointRounding.ToZero ---------------------
1.5 => 1
2.5 => 2
3.5 => 3
2.51 => 2
2.48 => 2
----------------Round with MidpointRounding.ToNegativeInfinity ---------------------
1.5 => 1
2.5 => 2
3.5 => 3
2.51 => 2
2.48 => 2
----------------Round with MidpointRounding.ToPositiveInfinity ---------------------
1.5 => 2
2.5 => 3
3.5 => 4
2.51 => 3
2.48 => 3
独行侠 2024-07-15 10:31:41

您可能需要检查的一件事是 Math.Round 的舍入机制:

http://msdn.microsoft.com/en-us/library/system.midpointrounding.aspx

除此之外,我建议使用 Math.Round(inputNumer, numberOfPlaces) 方法而不是 *100/100 方法,因为它是清洁工。

One thing you may want to check is the Rounding Mechanism of Math.Round:

http://msdn.microsoft.com/en-us/library/system.midpointrounding.aspx

Other than that, I recommend the Math.Round(inputNumer, numberOfPlaces) approach over the *100/100 one because it's cleaner.

酷到爆炸 2024-07-15 10:31:41

您应该能够使用 Math.Round(YourNumber, 2) 指定要舍入的位数。

您可以阅读更多内容 此处

You should be able to specify the number of digits you want to round to using Math.Round(YourNumber, 2)

You can read more here.

赠我空喜 2024-07-15 10:31:41

数学底线(123456.646 * 100) / 100
将返回 123456.64

Math.Floor(123456.646 * 100) / 100
Would return 123456.64

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文