CSharp: How to format a double value according to culture-specific formatting in Unity C#

  • en-US (US English) 🇺🇸
  • de-DE (DE Deutsch) 🇩🇪
  • In “en-US” period (.) is used to separate the decimals.
  • For example, $100.00 or 10.00
  • In “de-DE” comma (,) is used to separate the decimals.
    For example, 100,00€ or 10,00
  • In “en-US” comma (,) is used to separate the thousands.
  • For example, $1,000.00 or 1,000.00
  • In “de-DE” period (.) is used to separate the thousands.
    For example, 1.000,00€ or 1.000,00
using System.Collections.Generic;
using System.Globalization;

public class ExampleFormatDoubleValues : MonoBehaviour
{

double inputValueDouble = 79.00;

string outputValueDoubleString = string.Format(CultureInfo.InvariantCulture, "{0}", inputValueDouble);

// Output according to different cultures
// if culture is en-US then output is 79.00
// if culture is de-DE then output is 79,00

}
  • the value 79.00 is stored in inputValueDouble.
  • It is converted using the method String.Format MethodFormat(IFormatProvider, String, Object[])
  • The IFormatPorvider is CultureInfo.InvariantCulture
  • The string is "{0}
  • The Object[] is inputValueDouble
  • The converted value is stored in outputValueDoubleString

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akarsh SEGGEMU

Akarsh SEGGEMU

Software Engineer (Android, iOS, Unity) | M.Sc. Computer Science — Technische Universitaet Berlin | Mobile Advertising, Attribution, Monetisation, UA & Analytic