Domanda

I have code which contains XML which is parsed and treats all values as strings. Some of these strings are metric values with currency or % or number formatting. How can I obtain this formatting from the string and then reapply it later. The formatting is removed when numbers are cast to doubles.

<p><val>$4500.30</val><val>45.0%</val></p>

//parse out

String metric1  = "$4500.30";
String metric2  = "45.0%";

//remove special char
metric1 = metric1.replaceAll("[^A-Za-z0-9.]", "");

//I need to reapply formatting after it is removed(such as $)
È stato utile?

Soluzione 2

I realized this was a poor approach. Since the values were stored as string I just kept the formatting and used another copy of the string later which was converted to double.

Altri suggerimenti

You can also use a predefined instances of NumberFormats to be able to parse and format values every time you need. It's better then to have values both as plain & formatted values.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top