We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I want to change the format of the numbers with decimals because it is confusing when i have thousands (1,900) and decimals.
Generally a mere "1.900,35".replace(',', '.'); would suffice: http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#replace(char, char)
"1.900,35".replace(',', '.');
But when it contains both ',' & '.', we're gonna need a more sophisticated "swap" util function:
','
'.'
// forum.processing.org/two/discussion/11106/ // how-can-i-change-the-format-of-the-numbers-from-1-9-one-point-nine-to-1-9 static final String commaPointSwap(CharSequence s) { StringBuilder sb = new StringBuilder(s); for (int i = sb.length(); i-- != 0; ) { char ch = sb.charAt(i); if (ch == ',') sb.setCharAt(i, '.'); else if (ch == '.') sb.setCharAt(i, ','); } return sb.toString(); } void setup() { println( commaPointSwap("1.900,35") ); exit(); }
Alternative version using toCharArray() in place of a StringBuilder: B-)
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#toCharArray() http://docs.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html
// forum.processing.org/two/discussion/11106/ // how-can-i-change-the-format-of-the-numbers-from-1-9-one-point-nine-to-1-9 static final String commaPointSwap(String s) { char[] chars = s.toCharArray(); for (int i = chars.length; i-- != 0; ) { char ch = chars[i]; if (ch == ',') chars[i] = '.'; else if (ch == '.') chars[i] = ','; } return new String(chars); } void setup() { println( commaPointSwap("1.900,35") ); exit(); }
thanks!!!! :D
that is probably not the right way to do things.
better would be to use java's NumberFormat and supply either a Locale that suits your purposes or a custom format.
http://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html
https://docs.oracle.com/javase/tutorial/java/data/numberformat.html
Answers
Generally a mere
"1.900,35".replace(',', '.');
would suffice:http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#replace(char, char)
But when it contains both
','
&'.'
, we're gonna need a more sophisticated "swap" util function:Alternative version using toCharArray() in place of a StringBuilder: B-)
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#toCharArray()
http://docs.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html
thanks!!!! :D
that is probably not the right way to do things.
better would be to use java's NumberFormat and supply either a Locale that suits your purposes or a custom format.
http://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html
https://docs.oracle.com/javase/tutorial/java/data/numberformat.html