Parse negative value with thousands separator and decimal. : float parse « Data Types « C# / C Sharp






Parse negative value with thousands separator and decimal.

 

using System;
using System.Globalization;
using System.Threading;

public class ParseString
{
   public static void Main()
   {
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");

      string value;
      NumberStyles styles;

      value = "(4,3.64)";
      styles = NumberStyles.AllowParentheses | NumberStyles.AllowTrailingSign | NumberStyles.Float; 
      ShowNumericValue(value, styles);
   }

   private static void ShowNumericValue(string value, NumberStyles styles)
   {
      Single number;
      try
      {
         number = Single.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());
      }
   }   
}

   
  








Related examples in the same category

1.Parse a string in exponential notation with only the AllowExponent flag
2.Parse a string in exponential notation with the AllowExponent and Number flags.
3.Parse a currency value with leading and trailing white space, and white space after the U.S. currency symbol.
4.Parse a floating-point value with a thousands separator.
5.Parse a floating-point value with a currency symbol and a thousands separator.
6.Parse value in exponential notation.
7.Converts string to float
8.Converts string in a specified style to float
9.NumberStyles.AllowParentheses | NumberStyles.AllowTrailingSign | NumberStyles.Float | NumberStyles.AllowThousands
10.Converts string in a specified style and culture-specific format to float
11.Read value from console and convert it to float
12.Parse currency value using en-GB culture.
13.Convert string to float with NumberStyles.AllowDecimalPoint
14.System.Globalization.NumberStyles.AllowDecimalPoint | System.Globalization.NumberStyles.AllowThousands;
15.Parse a negative integer value.