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

Akarsh Seggemu
2 min readMar 27, 2022

string is the most preferred data type when passing values between two systems (for example, client-to-server / server-to-client). But string data type values cannot be used directly to perform mathematical operations. To do mathematical operations, the values need to be converted into any one of the following three number data types,

  • float
  • double
  • int

To convert the values into the above data types. You need to first do the safe conversion method TryParse.

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.

TryParse is available as a method in the three number data types.

This is how you do parsing of int and double value using C# in Unity project,

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

In the above code example,

  • 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;

To learn more, refer to System.Collections.Generic Namespace and CultureInfo Class.

I hope my article helps you understand how to parse int and double data types in Unity using C#.

If you like my articles, please follow me. You can also support me by buying me a coffee.

--

--

Akarsh Seggemu

IT Team Lead | MSc in Computer Science from Technische Universität Berlin | Writing articles to Empower Software Engineers and IT Leaders