简体   繁体   English

如何在C#中将点精度添加到十进制?

[英]How to add point precision to decimal in C#?

I would like to know how I can add point precision to decimal. 我想知道如何将点精度加到十进制。 I made a small program to calculate the weekly pay for workers but in some cases when the results are like $145.60, it shows $145.6 but I want the program to show the result $145.60. 我制作了一个小程序来计算工人的每周工资,但在某些情况下,当结果为$ 145.60时,它显示为$ 145.6,但我希望该程序将结果显示为$ 145.60。

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int hours;
            decimal total;
                Console.Write("Total working hours: ");
                hours = int.Parse(Console.ReadLine());
                total = Convert.ToDecimal(hours * 7.50);
                if (hours > 40)
                total = total + Convert.ToDecimal((hours - 40) * 3.75);
                Console.WriteLine("Total weekly pay is $" + total + ".");
                Console.ReadLine();
        }
    }
}

Firstly, fix this line and similar ones: 首先,修复此行和类似的行:

total = Convert.ToDecimal(hours * 7.50);

In this particular case it's unlikely to be a problem, but you should try to avoid mixing double and decimal . 在这种特殊情况下,这不太可能成为问题,但是您应避免混用doubledecimal You can just uese: 您可以使用:

decimal total = hours * 7.5m;

Likewise: 同样地:

if (hours > 40)
{
    total += (hours - 40) * 3.75m;
}

Next, for formatting the decimal, you can convert your whole Console.WriteLine into using a format string - which avoids the ugly string concatenation - and specify the number of digits in the format specifier: 接下来,要格式化小数点,您可以将整个Console.WriteLine转换为使用格式字符串-避免丑陋的字符串连接-并在格式说明符中指定位数:

Console.WriteLine("Total weekly pay is ${0:0.00}.", total);

The 0.00 is a custom numeric format string which forces there to always be at least one digit before the decimal point, and always exactly two after the decimal point. 0.00是一个自定义数字格式字符串 ,它强制始终在小数点前至少有一位数字,并始终在小数点后始终正好是两位数字。 In your case there would never be more than two anyway, but if your overtime became 3.755 for example, it would round. 在您的情况下,反正不会超过两个,但是例如,如果您的加班时间变为3.755,它将四舍五入。 You'd have to check whether it rounded in the way you wanted it to though - you might want to round it explicitly instead. 您必须检查它是否按照您想要的方式进行了四舍五入-您可能想显式地对其进行四舍五入。

As noted in comments, you could use the N2 standard numeric format string instead, if you wanted. 如注释中所述,如果需要,可以改用N2标准数字格式字符串 They will give different behaviour when the pay is over $1000 - N2 will give you grouping separators (eg $1,234.56) whereas 0.00 won't. 他们会给出不同的行为,当薪水超过$ 1000个- N2会给你而分组分隔符(如1,234.56 $) 0.00不会。

Another alternative to consider is removing the hard-coding of the $ and use the c standard numeric format, for currency: 考虑的另一种选择是删除$的硬编码,并使用c标准数字格式表示货币:

Console.WriteLine("Total weekly pay is {0:c}.", total);

This will use the current culture's default way of representing currency. 这将使用当前区域性的默认货币表示方式。 That means you've got to be using the right culture to start with, of course - I'm a UK user, so on my machine it uses £ instead of $... but if you know that the culture is appropriate for both the text and the currency, that's probably the cleanest approach. 当然,这意味着您必须先使用正确的文化习惯-我是英国用户,所以在我的机器上,它使用£而不是$...。但是,如果您知道这两种文化都适合文字和货币,这可能是最干净的方法。

This will help: 这将有助于:

decimal d = Convert.ToDecimal(3.10);
Console.WriteLine(d.ToString("#.00"));

The result will be 3.10 结果将是3.10

Use string formatting to format the output 使用字符串格式来格式化输出

 Console.WriteLine("Total weekly pay is $ {0:N2} .",total);

Check this link 检查此链接

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

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