CSharp: How to parse int and double value in Unity C#

Converts the string representation of a number in a specified style and culture-specific format to its double-precision/floating-point number/integer equivalent. A return value indicates whether the conversion succeeded or failed.

string inputValueDoubleString = "199.00";
string inputValueIntString = "20";

double outputValueDouble;
double outputValueInt;

int.TryParse(string.Format(CultureInfo.InvariantCulture, "{0}", inputValueDoubleString), NumberStyles.Any, CultureInfo.InvariantCulture, out outputValueDouble);

double.TryParse(string.Format(CultureInfo.InvariantCulture, "{0}", inputValueIntString), NumberStyles.Any, CultureInfo.InvariantCulture, out outputValueInt);

// Output according to different cultures
//
// For outputValueDouble
// if culture is en-US then output is 199.00
// if culture is de-DE then output is 199,00
//
// For outputValueInt
// if culture is en-US then output is 20
// if culture is de-DE then output is 20
// imports for the above code

using System.Collections.Generic;
using System.Globalization;

--

--

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