1

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();
        }
    }
}
daniele3004
  • 13,072
  • 12
  • 67
  • 75
Eion
  • 60
  • 1
  • 9

3 Answers3

3

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 (e.g. $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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Any reason you would use format string of `0.00` instead of `N2` (other than group separators)? – DavidG Oct 08 '14 at 10:18
  • @DavidG: Not particularly - I find it simpler to think about and recognise, but both will work. Will mention that. – Jon Skeet Oct 08 '14 at 10:20
  • Glad it's not just me then! I've always preferred to be explicit rather than having to remember N/F/G/etc but feel like I'm ignoring a framework construct if I do. – DavidG Oct 08 '14 at 10:22
  • Thanks a lot for your elaborate reply. Not only did it solve my problem, but it also enriched my knowledge. i will try to follow the conventions you stated in your reply and would try to construct more appropriate codes. – Eion Oct 08 '14 at 10:22
  • @DavidG: :) And of course, the `c` format may be more appropriate still - see my edit. – Jon Skeet Oct 08 '14 at 10:23
  • Reading it right now. Thanks again for taking your time to help me out. :) – Eion Oct 08 '14 at 10:25
0

This will help:

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

The result will be 3.10

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
oussama abdou
  • 317
  • 1
  • 7
0

Use string formatting to format the output

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

Check this link

Community
  • 1
  • 1
Prasanth V J
  • 1,126
  • 14
  • 32