0

I'm developing a website for a German Customer. In Germany, they use comma as decimal separator.

I use a WebMethod to get the values from SQL and then build a JSON object to show the data on the website. Using C# SqlCommand, I got the value from SQL as a string. I want to save this value into a double variable.

This is the code example which shows what I want to do:

    NumberFormatInfo nfi = new CultureInfo( "de-DE", false ).NumberFormat;
    nfi.NumberDecimalSeparator = ","; // Displays the value with a comma as the separator.

    string value ="15.95"; //value from SQL
    string valueInString = "";
    double valueInDouble = 0;

    valueInString = Convert.ToDouble(value.ToString()).ToString( "N", nfi );
    valueInDouble = Convert.ToDouble(value.ToString()).ToString( "N", nfi ); //Error
    valueInDouble = Convert.ToDouble(valueInString,nfi);
    Console.WriteLine( valueInString ); // returns 15,95. But it is a string
    Console.WriteLine( valueInDouble ); // returns 15.95. the comma is reverted back to dot

I need to save the data as double. How do I resolve this?

user2018
  • 310
  • 2
  • 7
  • 21
  • 2
    Decimals have no format,either in .NET or SQL. Formats apply only when they are converted to strings or parsed from strings. – Panagiotis Kanavos Nov 07 '18 at 12:36
  • BTW that `valueInDouble = Convert.ToDouble(value.ToString()).ToString( "N", nfi );` is a compilation error. The code is trying to assign a *string* into a *double* variable – Panagiotis Kanavos Nov 07 '18 at 12:38
  • @PanagiotisKanavos Thanks for your info. Does it mean that I have to use string if I want to show decimal with a comma? About your 2nd comment: Yes. Hence the //error comment at the end of the command :). That's what I originally wanted to achieve – user2018 Nov 07 '18 at 12:41
  • I suspect the *real* question is about localization and internationalization in .NET applications. That's already covered in docs and tutorials. ASP.NET can pick the *end user's* language preferences, to set the request thread's culture, both the UI culture and thread culture can be configured in `web.config` or the page itself. This can also be a user preference used to configure the request thread's cultures – Panagiotis Kanavos Nov 07 '18 at 12:41
  • It means that you probably have to do nothing except remove any attempt at formatting - if the end user had German as the first language in the browser's settings, it would be picked up and used to format values. You haven't specified *which* ASP.NET stack you use so it's not possible to point to specific instructions. WebForms, MVC, Core have differences – Panagiotis Kanavos Nov 07 '18 at 12:43
  • Check for example [this question](https://stackoverflow.com/questions/18826282/detecting-browser-display-language). One example shows how to use the browser's language - just leave or set the page's settings to auto (the default). The other, picks the desired language from a URL parameter – Panagiotis Kanavos Nov 07 '18 at 12:45
  • Also check [How to: Set the Culture and UI Culture for ASP.NET Web Page Globalization](https://msdn.microsoft.com/en-us/library/bz9tc508.ASPX). The link is for Webforms but localization works the same more or less, with configuration changes for each stack. – Panagiotis Kanavos Nov 07 '18 at 12:47
  • Another option for MVC - [this question](https://stackoverflow.com/questions/1560796/set-culture-in-an-asp-net-mvc-app) creates an ActionFilter that pulls the language parameter from the URL and uses it for each request – Panagiotis Kanavos Nov 07 '18 at 12:49
  • My browser is already in the German Language, but it shows dot as decimal separator. I also already use localization for my website to change the label based on the language. – user2018 Nov 07 '18 at 12:56
  • Post your *web page* code. How are you displaying the numbers (not strings)? Did you use binding, or hand-coded conversions to strings? Did you use any specific page directives? *What stack are you using*? It's not possible to help when there's no information, no code – Panagiotis Kanavos Nov 07 '18 at 13:01
  • In a separate comment you said you are generating *JSON*. That makes this a *different* question, unrelated to .NET - how to format numbers in Javascript based on the user's language, or whatever setting is required. What does the *Javascript* code look like in this case. – Panagiotis Kanavos Nov 07 '18 at 13:05
  • Well, I thought I have to convert it right when I get the data in the WebMethod and the pass the converted decimal to JS. I will open another question for my JS code and JSON object. – user2018 Nov 07 '18 at 13:11
  • 1
    Instead of opening another question search for duplicates. There are a lot of them, for example [this one](https://stackoverflow.com/questions/5314237/is-there-a-functionality-in-javascript-to-convert-values-into-specific-locale-fo). You can use [Number.toLocaleString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString) to format numbers in newer browsers, or third-party libraries/polyfills in older ones – Panagiotis Kanavos Nov 07 '18 at 13:17

1 Answers1

5

double does not contain any information about how to (visually) separate the decimal from the integral part. If you print a double without any specific culture format info, you get the standard . as a separator.

adjan
  • 13,371
  • 2
  • 31
  • 48
  • Thanks for your answer. I did use NumberFormatInfo for the culture format info though. Does that mean I have no choice but to use string instead of double when I want to show comma on the website? – user2018 Nov 07 '18 at 12:44
  • 1
    @User2018: The code that displays the double on the website should be responsible for applying the correct culture to the (possibly implicit) conversion to string. If you show us the relevant code, we can help you here. – Heinzi Nov 07 '18 at 12:45
  • @Heinzi I created a C# Object. Serialize (or deserialize?) it into a JSON object then render it to the website. I started to think maybe I need to convert the decimal using replace on the JS – user2018 Nov 07 '18 at 12:58
  • @User2018 that's a completely different question - numbers in JSON have a **specific** format. You can't have a number without a dot for decimal. If you want to display them in a certain way, it's your *Javascript code* that has to change and take localization into account – Panagiotis Kanavos Nov 07 '18 at 13:03
  • 1
    @User2018: In JavaScript, you can use [Number.toLocaleString](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString), see also https://stackoverflow.com/q/5882994/87698 – Heinzi Nov 07 '18 at 13:19