We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Variable results of split() and join()
Page Index Toggle Pages: 1
Variable results of split() and join()? (Read 730 times)
Variable results of split() and join()?
Nov 27th, 2009, 3:24am
 
Hello gang, I am new to Processing and Java, but otherwise I have a decent programming background. I came across a really puzzling problem.

First, a bit of useless background.  Here in Romania we write numbers in a completely opposite way from the rest of the world, meaning that we write a comma for a dot and a dot for a comma. E.g. for us 10.375,75 is "ten thousand three hundred and seventy-five point seventy-five". Don't ask me why, I think it's silly. Naturally, I wrote a function to perform the conversion:

Code:
String romFormat(String number){

 //See if there are commas
 String[] gazillions = split(number,',');
 int nParts = gazillions.length - 1;

 //See if there are dots
 String[] decimals = split(gazillions[nParts],".");
 gazillions[nParts] = join(decimals,",");
 String rom = join(gazillions, ".");
 return rom;
}


When I run romFormat(nfc(100000.00)) on my machine (Mac OS 10.5), I get the expected result (100.000,00).  BUT, on my other Mac, also a 10.5, I get 100,000,00 (in other words, the comma does not get converted to a dot).  On various Windows machines I get the same problem. What is going on?
Re: Variable results of split() and join()?
Reply #1 - Nov 27th, 2009, 5:07am
 
sip wrote on Nov 27th, 2009, 3:24am:
Here in Romania we write numbers in a completely opposite way from the rest of the world, meaning that we write a comma for a dot and a dot for a comma. [...] Don't ask me why, I think it's silly.

In France we used to use this notation, which is also used in a number of European countries, I believe (Spain, I think).
Now we use unbreakable space for break thousands, which is nice.

Strangely, I always found the US notation silly, instead... That, and putting month before day in dates, using Imperial measures, etc. :-P

Now, if I do:
println(nfc(10000000.01, 3));
on my French system, I get: 10 000 000,000
That's because Java is locale aware, and format numbers (when asked) using the local rules (which can be even different in some Asian countries).
So nfc() is nice to display user friendly values, but not for reuse later the values. The nfc() page should mention we can get something else than commas in output...

So the inconsistencies you get might come from inconsistencies in locale settings on your test computers.

A simpler way to get what you want is to put at the start of the sketch:
Locale.setDefault(Locale.US);
Re: Variable results of split() and join()?
Reply #2 - Dec 2nd, 2009, 12:06am
 
Thanks PhiLho, I should have known that our writing style is a French inheritance...  

I'll try your suggestion.  I guess it will make the display uniform accross all regions. And I will try your format with spaces instead of dots, I surely like it more.
Page Index Toggle Pages: 1