3

This question already has an answer here:

A typical piece of code printing a number to the console is this:

Console.WriteLine("The number is " + 1234.5 + ".");

This contains English text and formats the number using the locale of the current user. This can result in mixed output (here, German):

The number is 1234,5.

The application is not localized. It's user-visible text is English. How is this typically addressed? Is it OK to format numbers in the user's locale or is it better to force CurrentCulture to English?

This question is not about how to format a number in a culture specific way. It's about English text embedding possibly localized numbers.

marked as duplicate by Doc Brown, Robert Harvey, gnat, Dan Pichelman, Greg Burghardt Dec 7 '16 at 17:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Might be a better fit for User Experience – CodesInChaos Dec 7 '16 at 14:09
  • The behavior you describe in your question is normal: numbers are formatted using the locale of the current user. If you want different behavior, you have to format the number specifying a different culture. – Robert Harvey Dec 7 '16 at 14:17
  • Though you write the text in English it's not clear if British or American which have different number formatting. – qwerty_so Dec 7 '16 at 14:24
  • @DocBrown I clarified the question. – boot4life Dec 7 '16 at 16:33
  • 1
    You asked "is it ok to format number's in the user's locale" - and my answer (as written in that other question) is simply "yes if the result does not need to be machine-readable, no if it needs to be machine-readable, and also no if you are unsure about this". And if you need it in machine-readable form, use CultureInfo.InvariantCulture, not the CultureInfo of a specific country. But that is an implementation detail, better suited for SO. – Doc Brown Dec 7 '16 at 17:04
11

So if i understand you right, you have a non-localised application, which has text in English, but numbers, dates etc are automatically formatted to the CurrentCulture of the machine on which the program runs. Leading to mixed culture strings.

Obviously the best thing to do is full localise your application with resx files etc. But I assume this would be needlessly out of scope in your case.

Numbers can have quite subtle differences, you might argue that there is no problem, or indeed that it might be better to show them in the users culture. But consider datetimes:

System.Globalization.CultureInfo.CurrentCulture = new System
    .Globalization.CultureInfo("fr-FR");
Console.WriteLine(DateTime.UtcNow.ToString("U"));
//mercredi 7 décembre 2016 14:20:18

DateTimes can output full month and day names in the language specified. Presumably you would want all the text to be in english? and thus you should format it, and for consistency all numbers, currencies etc in the same culture as the text.

If you are using it throughout your app, then setting it once at the start would be a good idea


PS. Obviously the only correct English is "en-GB"


The Scope of CurrentCulture is per thread. You might want to check out msdn when deciding where to set it

https://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentculture(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentculture(v=vs.110).aspx#ThreadCulture

  • 1
    almost a +1 for the P.S. alone – Caleth Dec 7 '16 at 14:31
  • If one wants a "neutral" number format, he should use CultureInfo.InvariantCulture, not CultureInfo("en-GB"). – Doc Brown Dec 7 '16 at 17:07
  • i agree, but then i write dates yyyy-MM-dd – Ewan Dec 7 '16 at 17:11
  • 1
    @Caleth, only almost? :) – David Arno Dec 7 '16 at 19:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.