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;

--

--

--

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

GoLang: Simple PUB/SUB implementation using REDIS

Redis Pub Sub Implementation using GoLang

Go vs. Python — which one is better for machine learning? — WEBSENSA

Where Is Area Code 267 Coming From

Truth # 1. Apache Kafka on Raspberry PI…. working!.

Django static file cache, by the way of content hash

HTML5 + CSS3 | Footer Flex easy — 3 Columns

Playing with Traefik on Digital Ocean Kubernetes platform

Test-ready development

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 format a double value according to culture-specific formatting in Unity C#

The story of exception handling in C#

依赖注入概述

Instantiate() & Destroy()