Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Currency Symbol given by DecimalFormat looks invalid

Tags:

java

I face the below problem in java 8

import java.util.*;
import java.text.*;
import java.lang.*;

class NumberTest5 {
    public static void main(String[] args) {
        Locale loc = new Locale("sr","ME");
        DecimalFormat df = (DecimalFormat)NumberFormat.getCurrencyInstance(loc);
        System.out.println("\n"+"currencySymbol:"+df.getPositivePrefix()+"\tlength:"+df.getPositivePrefix().length());
        //here the above result is currencySymbol:  €+(non breakable space char)
        //length:2     
    }
}

the real question is why there is an extra character appended to the currency symbol ..?

why the above program behaves in this way ...?.

what is the problem in it & how to rectify it ..?

Thanks

like image 807
Andrews B Anthony Avatar asked Aug 02 '16 03:08

Andrews B Anthony


1 Answers

It's not invalid.

The following

Locale loc = new Locale("sr","ME");

represents the Locale for Serbian in Montenegro. I can't find the equivalent for Java, but here's a description of this locale for glibc. Under Currency, you'll notice Space separation between symbol and value is set to 1, indicating that

a space separates the symbol from the value

Therefore, if you formatted a value, for example

System.out.println(df.format(123.45));

you'd get

€ 123,45

with a space between the currency symbol at the value.

That's what the positive previx represents.

like image 64
Sotirios Delimanolis Avatar answered Sep 28 '22 03:09

Sotirios Delimanolis