Friday, 15 July 2011

java - Parsing a number from a string with thousands seperators -


I'm parsing a longer value than the given string-s

long.valouf () and long.parceleng () will not work - because such a string is str format in 1,234,567 and can be surrounded by spaces.

I can process this string by getting it a number of points - then parsing the value. Therefore - this will be something like this

  str.trim (); String Tempere [] = Straplers (","); Str = ""; (For string: tempArray) str + = s;  

However, there should be a better way of doing this. what is this?

You can use it right here US will be enough though you will not notice whitespace Will have to take care of.

  string str = "1,234,567"; Number format format = NumberFormat.getInstance (locale.US); System.out.println (format.parse (str.replace ("," "")));  

Whatever you are a bit more powerful, you can also use it. You can add a group or a decimal separator:

  decimal font format decimal decimal = new auspicious (); Decimal formattus slugs decimalable format symboils = new passwordfind symbo (); DecimalFormatSymbols.setGroupingSeparator (','); DecimalFormat.setDecimalFormatSymbols (decimalFormatSymbols); System.out.println (decimal format.php (str.replace ("," "")));  

No comments:

Post a Comment