简体   繁体   中英

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.

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 . 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("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. In your case there would never be more than two anyway, but if your overtime became 3.755 for example, it would round. 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. 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.

Another alternative to consider is removing the hard-coding of the $ and use the c standard numeric format, for currency:

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

Use string formatting to format the output

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

Check this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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