我如何用符号格式化土耳其里拉?
How can i format turkish lira with symbol?
我试着用语言环境来做,但它只显示为文本,符号不出来。我正在使用 JAVA 14 SDK。
我试过的代码:
Locale tr = new Locale("tr", "TR");
BigDecimal points = new BigDecimal(175678.64);
System.out.println(NumberFormat.getCurrencyInstance(tr).format(points));
输出:
175.678,64 TL
我要:
₺175.678,64
考虑使用 java.util.Currency
的 Currency
class。
Currency
class有两种方法getSymbol()
和getSymbol(Locale locale)
。在您的情况下,您应该使用带有 locale
参数的第二种方法。这将 return 一个 String
代表土耳其里拉的货币符号。
您可以像这样初始化 Currency
对象:
Currency currency = Currency.getInstance(tr);
和
currency.getSymbol(tr);
将return货币符号作为String
此外,您应该知道土耳其里拉符号在 Java 中作为字符的 unidoce 表示是 \u20BA
尝试对您所在的国家/地区
进行此更改 'Locale.CANADA'
NumberFormat format = NumberFormat.getCurrencyInstance(Locale.CANADA);
String currency = format.format(number);
System.out.println("Currency in Canada : " + currency);
没问题
我将您的代码分成多行以便于调试。
当我 run it in the IdeOne.com 站点时,我得到了您想要的输出。
顺便说一句,您应该将输入的数字作为文本传递(添加引号)。否则你就违背了使用 BigDecimal 的目的。
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.* ;
import java.text.* ;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Locale locale = new Locale("tr", "TR");
BigDecimal points = new BigDecimal( "175678.64" ) ;
NumberFormat f = NumberFormat.getCurrencyInstance( locale ) ;
String output = f.format( points ) ;
System.out.println( output ) ;
}
}
当运行:
₺175.678,64
我试着用语言环境来做,但它只显示为文本,符号不出来。我正在使用 JAVA 14 SDK。
我试过的代码:
Locale tr = new Locale("tr", "TR");
BigDecimal points = new BigDecimal(175678.64);
System.out.println(NumberFormat.getCurrencyInstance(tr).format(points));
输出:
175.678,64 TL
我要:
₺175.678,64
考虑使用 java.util.Currency
的 Currency
class。
Currency
class有两种方法getSymbol()
和getSymbol(Locale locale)
。在您的情况下,您应该使用带有 locale
参数的第二种方法。这将 return 一个 String
代表土耳其里拉的货币符号。
您可以像这样初始化 Currency
对象:
Currency currency = Currency.getInstance(tr);
和
currency.getSymbol(tr);
将return货币符号作为String
此外,您应该知道土耳其里拉符号在 Java 中作为字符的 unidoce 表示是 \u20BA
尝试对您所在的国家/地区
进行此更改 'Locale.CANADA'NumberFormat format = NumberFormat.getCurrencyInstance(Locale.CANADA);
String currency = format.format(number);
System.out.println("Currency in Canada : " + currency);
没问题
我将您的代码分成多行以便于调试。
当我 run it in the IdeOne.com 站点时,我得到了您想要的输出。
顺便说一句,您应该将输入的数字作为文本传递(添加引号)。否则你就违背了使用 BigDecimal 的目的。
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.* ;
import java.text.* ;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Locale locale = new Locale("tr", "TR");
BigDecimal points = new BigDecimal( "175678.64" ) ;
NumberFormat f = NumberFormat.getCurrencyInstance( locale ) ;
String output = f.format( points ) ;
System.out.println( output ) ;
}
}
当运行:
₺175.678,64