Currency symbol specified by DecimalFormat looks invalid

I am facing a problem below 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 that an extra symbol is added to the currency symbol.?

why does the above program behave this way ...?.

what is the problem and how to fix it ..?

thanks

+5
source share
2 answers

This is not valid.

Following

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

represents Locale for Serbian in Montenegro. I can not find the equivalent for Java, but here is a description of this language for glibc. In the "Currency" section, you will notice that Space separation between symbol and value set to 1 , which means that

space separates character from value

Therefore, if you formatted the value, for example

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

You'll get

 € 123,45 

with a space between the currency symbol in the value.

What is a positive prefix.

+6
source

The getPositivePrefix () method returns a string with a currency symbol and a space character. They do this in order to have a currency symbol and value shared.

Don't you need a space character? You can do two things:


Solution 1

 String symbol = df.getDecimalFormatSymbols().getCurrencySymbol(); System.out.println("\n"+"currencySymbol:"+ symbol + "500\tlength:"+symbol.length()); 

Output:

 currencySymbol:€500 length:1 

Decision 2

Add a escape escape sequence: '\ b', this will remove space during printing. But the size of the string length will remain when the length () method is called

 String symbol = df.getPositivePrefix(); System.out.println("\n"+"currencySymbol:" + symbol +"\b500\tlength:"+symbol.length()); 

Output:

 currencySymbol:€500 length:2 
0
source

All Articles