Sweden Currency validation

Hello ,

Can any one tell me why the following is not retuning (0)

as indication that S and S2 Strings are equal?

java.text.NumberFormat currency = java.text.NumberFormat.getCurrencyInstance(new java.util.Locale("sv","SE" ) );

String S ="1 234,56";

String S2= (String)currency.format( 1234.56 );

System.out.println(? S is >> ? S);

System.out.println(? S2 is >> ? S2);

return (S.compareTo(S2));

Knowing that formatting 1234.45 in Sweden locale should be 1 234,45

[685 byte] By [tazo4ua] at [2007-9-30 23:35:06]
# 1

I have to admit that I haven't done much Localization of currency values but I'm not sure why you expect these two strings to return 0 when invoking compareTo. When I run your code I get the following output:

S is >> 1 234,56

S2 is >> 1.234,56 kr

-14

These strings certainly are different, I'm assuming the kr stands for krona, the name for swedish currency. Based on the Javadoc describing compareTo I would expect these strings to generate a non-zero value (since they are not equal). Maybe I'm missing something, why do you expect compareTo to return 0?

caseyloua at 2007-7-7 14:49:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

It is documented in the String.compareTo(String) method API that the compareTo method will only return "0" if both strings are equal ie. String.equals() methods returns true.

Now coming back to your problem, if you invoke S.equals(S2) then it will return false. The reason behind returning false is that both S and S2 strings length are same and appears also the same when you print them on standard out but both strings 2 character does not match. For S the int value of second character is "32" where for S2 the second character value is "128".

I hope this will clarify you.

~Mitul

lmitula at 2007-7-7 14:49:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
Yes one more thing, if you change S to "1 234,56 kr" then also it will give non-zero value because the fact is that the second character in both strings have different value.
lmitula at 2007-7-7 14:49:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
I'm a little confused by this. I get a length of 8 for S and 11 for S2.
caseyloua at 2007-7-7 14:49:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5
Thanks for your accurate respond, but that is actualy the heart of my question , why is the seond char in both are different , knowing that both are "spaces"?
tazo4ua at 2007-7-7 14:49:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6
assume S = "1 234,56 kr";still both S and S2 are not equal and the only difference is in the second char from the left which is space in both stirngs.
tazo4ua at 2007-7-7 14:49:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7

Please run below piece of code to know what exactly is going on. If you have any doubts feel free to contact me.

java.text.DecimalFormat currency = (DecimalFormat)java.text.NumberFormat.getCurrencyInstance(new java.util.Locale("sv", "SE" ) );

DecimalFormatSymbols symbols = currency.getDecimalFormatSymbols();

System.out.println((int)symbols.getGroupingSeparator());

System.out.println((int)' ');

String S = "1 234,56";

String S2= (String)currency.format( 1234.56 );

System.out.println(? S is >> ? S);

System.out.println(? S2 is >> ? S2);

return (S.compareTo(S2));

cheers:)

~Mitul

lmitula at 2007-7-7 14:49:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 8
I can clearly see the difference, which would pause my second question..if I have a user in my multi currency supported app entered 1 234,45 in price field in Sweden store, how could I validate that using swedish locale?
tazo4ua at 2007-7-7 14:49:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 9

Check out the below code I hope that you will get what you want now.

java.text.DecimalFormat currency = (DecimalFormat)java.text.NumberFormat.getCurrencyInstance(new java.util.Locale("sv", "SE" ) );

DecimalFormatSymbols symbols = new DecimalFormatSymbols(new java.util.Locale("sv", "SE" ) );

symbols.setGroupingSeparator((char)32);

currency.setDecimalFormatSymbols(symbols);

System.out.println((int)symbols.getGroupingSeparator());

System.out.println((int)' ');

String S = "1 234,56 kr";

String S2= (String)currency.format( 1234.56 );

System.out.println(? S is >> ? S);

System.out.println(? S2 is >> ? S2);

return (S.compareTo(S2));

I would suggest you that it is a good practise to read and understand the use of API carefully, which will save a lot of time.

~Mitul

lmitula at 2007-7-7 14:49:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 10
great, that worksThanks man
tazo4ua at 2007-7-7 14:49:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...