[英]How can i format turkish lira with symbol?
I tried to do it with the locale, but it only appears as text and the symbol doesn't come out.我试图用语言环境来做,但它只显示为文本,符号不出来。 I am using JAVA 14 SDK.
我正在使用 JAVA 14 SDK。
Code I tried:我试过的代码:
Locale tr = new Locale("tr", "TR");
BigDecimal points = new BigDecimal(175678.64);
System.out.println(NumberFormat.getCurrencyInstance(tr).format(points));
Output:输出:
175.678,64 TL
I want:我想要:
₺175.678,64
I broke out your code to multiple lines for easier debugging.我将您的代码分成多行以便于调试。
When I run it in the IdeOne.com site, I get your desired output.当我在 IdeOne.com站点上运行它时,我得到了你想要的输出。
By the way, you should pass your input number as text (add quote marks).顺便说一下,您应该将输入的数字作为文本传递(添加引号)。 Otherwise you are defeating the purpose of using BigDecimal.
否则,您就违背了使用 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 ) ;
}
}
When run:运行时:
₺175.678,64
₺175.678,64
Consider using the Currency
class of java.util.Currency
.考虑使用
java.util.Currency
的Currency
类。 The Currency
class has two methods getSymbol()
and getSymbol(Locale locale)
. Currency
类有两个方法getSymbol()
和getSymbol(Locale locale)
。 In your case you should use the second method with the locale
parameter.在您的情况下,您应该使用带有
locale
参数的第二种方法。 This will return a String
that represents the currency symbol for turkish lira.这将返回一个代表土耳其里拉货币符号的
String
。
You can initialize your Currency
object like this:您可以像这样初始化您的
Currency
对象:
Currency currency = Currency.getInstance(tr);
and和
currency.getSymbol(tr);
will return the currency symbol as a String
将货币符号作为
String
返回
In addition, you should know that the unidoce representation of turkish lira symbol as a char in Java is \₺
此外,您应该知道,在 Java 中,土耳其里拉符号作为字符的 unidoce 表示是
\₺
try this change 'Locale.CANADA' with your country尝试在您的国家/地区更改“Locale.CANADA”
NumberFormat format = NumberFormat.getCurrencyInstance(Locale.CANADA);
String currency = format.format(number);
System.out.println("Currency in Canada : " + currency);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.