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

--

--

--

Software Engineer - (Android, iOS and Unity) | M.Sc. Computer Science from Technische Universitaet Berlin | Mobile Advertising, attribution, monetisation

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Forms in Blazor

Golden — what activities do I have to do to get an airdrop?

I produced a coding tutorial, here’s what I learned.

How To Bypass That Goddamn CloudFlare

Unhandled errors in Go

How To Migrate From BigCommerce to Shopify

How To Migrate From BigCommerce to Shopify

Percentage by Group and Dictionary to Columns in Pandas

What is Amazon VPC

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 and Unity) | M.Sc. Computer Science from Technische Universitaet Berlin | Mobile Advertising, attribution, monetisation

More from Medium

How to parse int and double value in Unity C#

What is Maven?

Remembering TDD