How can i format turkish lira with symbol? - java

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.
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

No problem
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.
By the way, you should pass your input number as text (add quote marks). Otherwise you are defeating the purpose of using 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

Consider using the Currency class of java.util.Currency.
The Currency class has two methods getSymbol() and getSymbol(Locale locale). In your case you should use the second method with the locale parameter. This will return a String that represents the currency symbol for turkish lira.
You can initialize your Currency object like this:
Currency currency = Currency.getInstance(tr);
and
currency.getSymbol(tr);
will return the currency symbol as a String
In addition, you should know that the unidoce representation of turkish lira symbol as a char in Java is \u20BA

try this change 'Locale.CANADA' with your country
NumberFormat format = NumberFormat.getCurrencyInstance(Locale.CANADA);
String currency = format.format(number);
System.out.println("Currency in Canada : " + currency);

Related

Formatting currency with java8 and java 11

I have an issue where the format returned from the exact same methods are different in different java versions.
Currency currency = Currency.getInstance("CAD");
NumberFormat formatter = NumberFormat.getCurrencyInstance();
formatter.setCurrency(currency);
formatter.setMaximumFractionDigits(2);
formatter.setMinimumFractionDigits(2);
System.out.println(" " + formatter.format(10.00)) ;
It returns CA$10.00 in Java 11 but in Java 8 it's CAD10.00.
Is there any way I can get the prefix to always be CAD across Java versions? I know that currency.getCurrencyCode() returns CAD consistently across the versions. But I can't seem to find an overloaded method that let's me configure this.
The following works for me. Looking through the source code, it appears that the CA$ is the currency symbol. Seems like it was changed from CAD (in Java 8) to CA$ (in later versions). The following line of code actually returns a DecimalFormat instance.
NumberFormat formatter = NumberFormat.getCurrencyInstance();
You can explicitly set the currency symbol to CAD.
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Currency;
public class SchTasks {
public static void main(String args[]) {
Currency currency = Currency.getInstance("CAD");
NumberFormat formatter = NumberFormat.getCurrencyInstance();
formatter.setCurrency(currency);
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setCurrencySymbol("CAD");
((DecimalFormat) formatter).setDecimalFormatSymbols(dfs);
formatter.setMaximumFractionDigits(2);
formatter.setMinimumFractionDigits(2);
System.out.println(" " + formatter.format(10.00)) ;
}
}
You can run the above code from this JDoodle
https://www.jdoodle.com/iembed/v0/s67

How to use decimal number without creating a String resource

In Java and Kotlin there is an API that can used to show a time without having to create a string resource.
In the example line of code below, this value allows the time of 8 hours after midnight to automatically chnage the way its displayed depending on the device locale.
val timeCustom = LocalTime.of(8, 0)
Is there something similar that can be used for a decimal number, where the value automatically uses a specific demical symbol dpending on the locale? (. or ,).
For example, to describe the height of something (e.g. 5 point 2 metres):
val decimalNumber = Decimal.of(5,2)
Is there something like this available?
You can use Locale.getDefault() e.g.
import java.text.NumberFormat;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
Locale currentLocale = Locale.getDefault();
Integer quantity = 123456;
Double amount = 345987.246;
NumberFormat numberFormatter;
String quantityOut;
String amountOut;
numberFormatter = NumberFormat.getNumberInstance(currentLocale);
quantityOut = numberFormatter.format(quantity);
amountOut = numberFormatter.format(amount);
System.out.println(quantityOut);
System.out.println(amountOut);
}
}
Output:
123,456
345,987.246

Java Local Decimal Separator

I tried to get the local decimal separator with the following code:
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;
//option 1
DecimalFormat format=(DecimalFormat) NumberFormat.getInstance();
DecimalFormatSymbols symbols=format.getDecimalFormatSymbols();
public char sep=symbols.getDecimalSeparator();
//option 2
double value = 1234.5;
NumberFormat fmt = NumberFormat.getInstance();
String formatted = fmt.format(value);
public String decSep= formatted.substring(5,6);
//option 3
public Locale loc=Locale.getDefault();
DecimalFormatSymbols symbols= new DecimalFormatSymbols(loc);
public char sep=symbols.getDecimalSeparator();
public String decSep=Character.toString(sep);
but I always get "," even if the setting is set to "."
I tried with VB.NET code and it works.
What's wrong or missing in Java code ?
I appreciate your help
Try this :
Locale.setDefault(Locale.US);
This will set every output number with decimal separator. You can also take some input numbers with decimal separator.

NumberFormat vs Joda money

I need to display currency amount with the symbol and format based on currency code. Currently, I'm using a default locale for each currency code since I don't have access to the exact locale along with the currency code and using NumberFormat.format() to get the formatted currency amount with format and symbol. Does Joda money do this all - provide currency code and it displays the formatted currency with symbol? Any help/direction regarding this is appreciated.
I just found out about joda-money, I'm testing it to see if it fits in my project requirements. I read your question and decided to answer it while testing the library.
For what I could see inside the joda-money jar it has very few classes and provide the basic currency management and a formatter.
It seems that at the early stage in which joda-money is the formatter still needs the Locale to print the money symbol, as you can see in my code. (The code is in scala but the methods call are the same in Java)
import org.joda.money.format.MoneyFormatterBuilder
import org.joda.money.{Money, CurrencyUnit}
def formatterBuilder() = new MoneyFormatterBuilder().appendCurrencySymbolLocalized().appendAmount()
def moneyFormatter(locale: java.util.Locale) = formatterBuilder().toFormatter(locale)
def moneyFormatter() = formatterBuilder().toFormatter
val usd: CurrencyUnit = CurrencyUnit.of("USD")
val money = Money.of(usd, 23232312d) // or just Money.parse("USD 23232312")
moneyFormatter().print(money) // res0: String = USD23,232,312.00
moneyFormatter(java.util.Locale.US).print(money) // res1: String = $23,232,312.00
As you can see, the Locale is needed to print the '$' symbol.
Additionally I tried with another currency, the yen (Japan currency). I printed it with the US locale and the result was something I didn't spec.
val japan = Money.parse("JPY 23232312")
moneyFormatter().print(japan) // res2: String = JPY23,232,312
moneyFormatter(java.util.Locale.JAPAN).print(japan) // res3: String = ¥23,232,312
moneyFormatter(java.util.Locale.US).print(japan) // res4: String = JPY23,232,312
EDIT:
You could also create an abstract class as a wrapper for Money, like this:
abstract class Currency(amount: BigDecimal, locale: java.util.Locale) {
val currencyUnit: CurrencyUnit = CurrencyUnit.getInstance(locale)
val money: Money = Money.of(currencyUnit, amount)
def formatted: String = new MoneyFormatterBuilder().appendCurrencySymbolLocalized().appendAmount().toFormatter(locale).print(money)
// implement others Money methods
}
class USDollars(amount: BigDecimal) extends Currency(amount, java.util.Locale.US)
public static void main(String[] args) throws Exception {
Currency usd = java.util.Currency.getInstance("USD");
NumberFormat format = java.text.NumberFormat.getCurrencyInstance(java.util.Locale.US);
format.setCurrency(usd);
System.out.println(format.format(23232312));
}
Output
$23,232,312.00

Java: altering locale formatting

I would like to modify the way my application locale formats and displays numbers.
In my case the locale is "it_IT", so numbers are formatted with '.' as GroupingSeparator and ',' as DecimalSeparator. Is it possible to alter or remove those symbols at global application level?
Depending on what do you want there a different ways. See some below...
1 set the default locale in your applicationLocale.setDefault(Locale.UK); this will use a comma as GroupingSeparator and a point as DecimalSeparator
2 you can create the java.text.NumberFormat for a specific locale NumberFormat.getInstance(Locale.UK), will behave like solution 1)
3 you can use one of the above an remove the GroupingSeparator
NumberFormat formatUK = NumberFormat.getInstance(Locale.UK);
formatUK.setGroupingUsed(false);
edit:
import java.text.NumberFormat;
import java.util.Locale;
public class Foo {
public static void main(String[] args) {
NumberFormat formatUK = NumberFormat.getInstance(Locale.UK);
double someValue = 1234567.8899;
System.out.printf("%16s: %15s%n", "with grouping", formatUK.format(someValue));
formatUK.setGroupingUsed(false);
System.out.printf("%16s: %15s%n", "without grouping", formatUK.format(someValue));
}
}

Categories