3

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.

Bart van Ingen Schenau
  • 71,712
  • 20
  • 110
  • 179
boot4life
  • 665
  • 1
  • 6
  • 8
  • Might be a better fit for [ux.se] – CodesInChaos Dec 07 '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 07 '16 at 14:17
  • Though you write the text in English it's not clear if British or American which have different number formatting. –  Dec 07 '16 at 14:24
  • @DocBrown I clarified the question. – boot4life Dec 07 '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 07 '16 at 17:04
  • @ThomasKilian, AFAIK, British and American numbers are formatted the same: 123,456.789 – David Arno Dec 07 '16 at 19:44
  • @DavidArno See https://en.wikipedia.org/wiki/Decimal_mark below in the History part –  Dec 07 '16 at 20:37
  • @ThomasKilian what about it? We've left the 60s long behind. Britain uses the full stop as the decimal separator these days. There's a difference in formatting dates, but not numbers. – David Arno Dec 07 '16 at 20:43
  • @DocBrown as far as I can tell the answer does not discuss localized numbers within non-localized text. – boot4life Dec 08 '16 at 16:21
  • Well, your text is english, isn't it? That is pretty localized. However, if you ask if it is sometimes **tolerable** to embed german number format in english text, your question would be closed either - not as a dupe, but as primarily opionated. This depends 100% on the audience. If it is a tool just for use in your dev team, it might be tolerable. If the values become ambiguous in interpretation, it might not be tolerable. Think about who are users, and what will they do with the output. There is no generally "better" way. – Doc Brown Dec 08 '16 at 16:33

1 Answers1

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

Ewan
  • 70,664
  • 5
  • 76
  • 161