15

I have a method which returns numbers like this:

public decimal GetNumber()
{
    return 250.00m;
}

Now when this value is printed to the console for example, it has a comma (250,00) instead of a point (250.00). I always want a point here, what am I doing wrong?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
grady
  • 12,281
  • 28
  • 71
  • 110
  • 6
    If you are unware: `,` is used as a decimal separator in many European countries, such as Germany, France and Italy. Perhaps you have your culture set to one of these place? – Matt Ellen Oct 06 '10 at 07:10
  • I've tested this function in a console application and works ok for me. Is displayed with point (250.00). – Andrei Bularca Oct 06 '10 at 07:13

4 Answers4

50

decimal itself doesn't have formatting - it has neither a comma nor a dot.

It's when you convert it to a string that you'll get that. You can make sure you get a dot by specifying the invariant culture:

using System;
using System.Globalization;
using System.Threading;

class Test
{
    static void Main()
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
        decimal d = 5.50m;
        string withComma = d.ToString();
        string withDot = d.ToString(CultureInfo.InvariantCulture);
        Console.WriteLine(withComma);
        Console.WriteLine(withDot);
    }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    System.Globalization.CultureInfo germanCultureInfo = new System.Globalization.CultureInfo("de-DE"); decimal d = 0.3m; string s1 = d.ToString(germanCultureInfo); -> s1 is 0.3 not 0,3 . Why? – Offler Jun 10 '15 at 15:18
  • 1
    @Offler: I can't reproduce that - that *exact* code gives me "0,3". – Jon Skeet Jun 10 '15 at 15:52
  • 1
    Happened at a customers machine. Was searching the Internet to find out why... Solution now found at customers pc - Windows Settings where changed. – Offler Jun 19 '15 at 17:48
  • 1
    I guess Convert.ToString() would be safer than d.ToString(); @JonSkeet – ManirajSS Jun 23 '15 at 09:00
  • @ManirajSS: Why do you "guess" that? In what way do you think it would be "safer"? – Jon Skeet Jun 23 '15 at 09:03
  • By referring this link i came to know `http://stackoverflow.com/a/326071/3186681` .Tostring() can throw exception if object is null.but Convert.Tostring() will handle null objects and return string.Empty. But in your answer `d` is non nullable.so no problem(if it is nullable it can throw error).Correct if i am worong legend. :) @JonSkeet – ManirajSS Jun 23 '15 at 09:22
  • @ManirajSS: Exactly, `d` is non-nullable, so there's no point in calling `Convert.ToString`. (Also, if it *were* nullable but not expected to be null, often an exception would be a better result than an empty string...) – Jon Skeet Jun 23 '15 at 09:42
7

As explained by Jon Skeet, you should specify the culture used to format the string:

var str = GetNumber().ToString(System.Globalization.CultureInfo.InvariantCulture);

It's a good practice to always use the ToString overload in which you specify the culture. Otherwise, .NET use the current thread Culture, which would write different strings to the output according to the locale of the PC...

Gonzalo.-
  • 12,512
  • 5
  • 50
  • 82
Eilistraee
  • 8,220
  • 1
  • 27
  • 30
  • 1
    ...on the other hand it could just as well be that using the current locale on the PC (given that we are not talking web application) is the preferred behavior. That would after all be what the user is used to seeing. – Fredrik Mörk Oct 06 '10 at 07:30
  • 2
    Yes, I agree with you. It's a valid behavior when the string will be used in the UI. But it can be a real issue when the string is used to perform some sort of serialization. In this case, the deserialization may fail depending on the executing PC locale... And you won't see this before the product is shipped on a platform with a different locale. I Prefer to take a few seconds to think about it (and take a reasonable decision about it) than regretting it later. – Eilistraee Oct 06 '10 at 09:29
1

Locale-specific formatting?

http://en.wikipedia.org/wiki/File:DecimalSeparator.svg (Green equals a comma, so if you are calling ToString() on your decimal using the culture info of any of these locations, you will see a comma).

Tim M.
  • 53,671
  • 14
  • 120
  • 163
0

I have checked it with visual studio 2008 (console application) and its not showing "," instead of "." , please provide more details. I think its issue of culture-info. please provide some more code details

class Program
{
    static void Main(string[] args)
    {
        Console.Write(GetNumber());
    }
    public static  decimal GetNumber()
    {
        return 250.00m;
    }

}
Dr. Rajesh Rolen
  • 14,029
  • 41
  • 106
  • 178