CSharp: How to parse int and double value in Unity C#
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 ininputValueDoubleString
as string data type. - The value parsed using the method —
TryParse(String, NumberStyles, IFormatProvider, Double)
String
isinputValueDoubleString
- First, it is formatted using the method String.Format Method —
Format(IFormatProvider, String, Object[])
- The
IFormatPorvider
isCultureInfo.InvariantCulture
- The
string
is"{0}
- The
Object[]
isinputValueDoubleString
IFormatPorvider
isCultureInfo.InvariantCulture
NumberStyles
isNumberStyles.Any
Double
isoutputValueDouble
- The converted value stored in
outputValueDouble
- The value
20
is stored ininputValueIntString
as string data type. - The value parsed using the method —
TryParse(String, NumberStyles, IFormatProvider, Double)
String
isinputValueIntString
- First, it is formatted using the method String.Format Method —
Format(IFormatProvider, String, Object[])
- The
IFormatPorvider
isCultureInfo.InvariantCulture
- The
string
is"{0}
- The
Object[]
isinputValueIntString
IFormatPorvider
isCultureInfo.InvariantCulture
NumberStyles
isNumberStyles.Any
Double
isoutputValueInt
- 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.