NumberFormat returns a negative ascii value

Formatting using NumberFormat class is returning an unidentified character, a negative ascii code number. The attached code snippet yields the following:

In the debugger, num looks like this: 5 000 $
bytes = 53 -96 48 48 48 32 36

What is the -96 doing in there? It's causing display problems for me. I've tried compiling under Java 1.5.0.4 and Java 1.6.0.16 and got same results.
Locale locale = new Locale("fr", "CA");
NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
String num = nf.format(5000.0);
byte[] bytes = num.getBytes();

Open in new window

brian-barnettAsked:
Who is Participating?
 
brian-barnettConnect With a Mentor Author Commented:
Looks like it is a non-breaking space issue with the NumberFormat implementation on a Windows box.

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4418200
0
 
CPColinSenior Java ArchitectCommented:
The byte type is signed in Java. That means it goes from -128 to 127 instead of 0 to 255. When printing them out, if you AND each byte with 0xFF, everything should show up fine.

So like instead of this:

System.out.print(bytes[i]);

do this:

System.out.print(bytes[i] & 0xFF);
0
 
brian-barnettAuthor Commented:
I don't actually need to put it into a byte array, I was just trying to figure out what the bad character was. After calling nf.format(5000.0), I end up taking that num value and return it to the client in an xml document. The error I am getting is:

XML Parsing Error: not well-formed
and it points to this-- beginningBalance="5¿000 $"

Any ideas why NumberFormat would do that? Am I not using NumberFormat correctly?
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
brian-barnettAuthor Commented:
The xml doc is UTF-8
0
 
CPColinSenior Java ArchitectCommented:
It looks like that weird character is supposed to be a non-breaking space. I guess the Canadian French locale uses that as the thousands separator. I don't know enough about XML to know whether that character is not allowed in an attribute. Does the problem persist if you use a different locale, like US English?
0
 
CPColinSenior Java ArchitectCommented:
Oops. You got it.
0
 
wannabetechieCommented:
The problem is with the usage of locale.I guess you want the language for the locale to be Canadian french
and the code for that is "fr_CA"

So change the locale definition to
Locale locale = new Locale("fr_CA", "CA");

And you should not get the junk character now.
0
 
brian-barnettAuthor Commented:
It does not happen with US english. It can be included in xml but needs special handling. It would be represented in xml like this -->  

wannabetechie:
I think your comment is erroneous. The constructor you specified for Locale accepts two params, a language and a country code. "fr_ca" is not a valid value for language. The correct syntax is as I showed in my original code snippet.

Locale locale = new Locale("fr", "CA");
0
 
CEHJCommented:
>>Looks like it is a non-breaking space issue with the NumberFormat implementation on a Windows box.

I get the same and don't use Windows. Is it a bug? Do have some locales have this weird NBSP in the format?
0
 
brian-barnettAuthor Commented:
I just read about here http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4418200. I experienced it with fr_CA locale (french canada). The link above describes the problem  with sv_SE (Sweden) as well, but I suspect it would happen with any currency that uses a space as the thousands separator. The idea is the NBSP keeps the number from being split across lines when being displayed.
0
 
CEHJCommented:
>>The idea is the NBSP keeps the number from being split across lines when being displayed.

Ah - i see
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.