C# .NET - Globalization in C#

Asked By Kishore Rajendran on 26-Apr-11 04:42 AM
I have two doubles value
i am assigning those values

 Collapse
Double planned,convert_hours,total;
planned=0.125;
convert_hours=1.525;
total=planned+convert_hours;


But its accepting values like 0,125 and 1,525 instead of 0.125 and 1.525

I think its because of globalisation problem..

IF yes,please help me to resolve this
TSN ... replied to Kishore Rajendran on 26-Apr-11 04:58 AM
Hi..

before adding Convert or parse the values as shown below..

public static void Main()
{
   // Set current thread culture to en-US.
   Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
 
   string value;
   NumberStyles styles;
   value = "(4,320.64)";
   styles = NumberStyles.AllowParentheses | NumberStyles.AllowTrailingSign |
      NumberStyles.Float;
   ShowNumericValue(value, styles);
 
   styles = NumberStyles.AllowParentheses | NumberStyles.AllowTrailingSign |
      NumberStyles.Float | NumberStyles.AllowThousands;
   ShowNumericValue(value, styles);
}
 
// the method would be..
 
private static void ShowNumericValue(string value, NumberStyles styles)
{
   double number;
   try
   {
    number = Double.Parse(value, styles);
    Console.WriteLine("Converted '{0}' using {1} to {2}.",
            value, styles.ToString(), number);
   }
   catch (FormatException)
   {
    Console.WriteLine("Unable to parse '{0}' with styles {1}.",
            value, styles.ToString());
   }
   Console.WriteLine();              
}  
the out put to the above ...
 
//    Unable to parse '(4,320.64)' with styles AllowTrailingSign, AllowParentheses, Float.
//    
//    Converted '(4,320.64)' using AllowTrailingSign, AllowParentheses, AllowThousands, Float to -4320.64.   
dipa ahuja replied to Kishore Rajendran on 26-Apr-11 11:41 AM
Write the code:
using System.Globalization;
NumberFormatInfo numberFormat = new NumberFormatInfo();
numberFormat.NumberDecimalSeparator = ",";
double value = Convert.ToDouble(planned, numberFormat); // or Double.Parse()
double value2 = Convert.ToDouble(convert_hours, numberFormat); // or Double.Parse()