Hi Guys
NP140 NumberFormatInfo and Double.Parse()
In the following program what is the function of NumberFormatInfo and Double.Parse() method. Because I am unable to explain the output of program.
Please explain.
Thank you
using System;
using System.Globalization;
//using System.Text;
public class NumParsingApp
{
public static void Main(string[] args)
{
string u = "AA -1,234,567.890 ";
NumberFormatInfo ni = new NumberFormatInfo();
ni.CurrencySymbol = "AA";
double h = Double.Parse(u, NumberStyles.Any, ni);
Console.WriteLine("h = {0:F}", h);
}
}
/*
h = -1234567.89
*/
Posted Aug 28, 2008, 7:46 AM
AlanPosted Aug 27, 2008, 7:37 PM
The NumberFormatInfo class is used to define how numbers are formatted in a particular culture. In this case it's used to tell double.Parse() that the number can be (though it doesn't have to be) preceded by a currency symbol "AA".
Armed with that info, double.Parse() is then able to parse the string "AA -1,234,567.890" to the double value -1234567.89. If NumerFormatInfo hadn't been used a format exception would have been thrown.