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

  • float
  • double
  • int
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
  • The value 199.00 is stored in inputValueDoubleString as string data type.
  • The value parsed using the method — TryParse(String, NumberStyles, IFormatProvider, Double)
  • String is inputValueDoubleString
  • First, it is formatted using the method String.Format MethodFormat(IFormatProvider, String, Object[])
  • The IFormatPorvider is CultureInfo.InvariantCulture
  • The string is "{0}
  • The Object[] is inputValueDoubleString
  • IFormatPorvider is CultureInfo.InvariantCulture
  • NumberStyles is NumberStyles.Any
  • Double is outputValueDouble
  • The converted value stored in outputValueDouble
  • The value 20 is stored in inputValueIntString as string data type.
  • The value parsed using the method — TryParse(String, NumberStyles, IFormatProvider, Double)
  • String is inputValueIntString
  • First, it is formatted using the method String.Format MethodFormat(IFormatProvider, String, Object[])
  • The IFormatPorvider is CultureInfo.InvariantCulture
  • The string is "{0}
  • The Object[] is inputValueIntString
  • IFormatPorvider is CultureInfo.InvariantCulture
  • NumberStyles is NumberStyles.Any
  • Double is outputValueInt
  • The converted value stored in outputValueInt
// 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
Akarsh SEGGEMU

Akarsh SEGGEMU

24 Followers

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