In this post we will talk and learn How would you print a given currency value for Indian Locale (INR Currency) in java?
- NumberFormat class is designed specifically for this purpose. Currency symbol & Rounding Mode is automatically set based on the locale using NumberFormat.
- Lets see an example..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.kkjavatutorials.util; import java.text.NumberFormat; import java.util.Locale; /** * @author KK JavaTotorials *Utility Class to print currency amount in Indian Locate */ public class CurrencyUtil { private static final NumberFormat numberFormat = NumberFormat.getCurrencyInstance(new Locale("en","in")); /** * @param amout input * @return amount in string */ public static String formatMonetaryAmount(double amout) { numberFormat.setMinimumFractionDigits(2); numberFormat.setMaximumFractionDigits(4); return numberFormat.format(amout); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.kkjavatutorials.client; import java.math.BigDecimal; import com.kkjavatutorials.util.CurrencyUtil; /** * @author KK JavaTutorials * Java program to print a given currency value for Indian Locale (INR Currency)? */ public class PrintCurrencyAmout { public static void main(String[] args) { BigDecimal bigDecimal = new BigDecimal(22.1214984); System.out.println("Amount = " + CurrencyUtil.formatMonetaryAmount(bigDecimal.doubleValue())); } } |
Output::
Amount = Rs.22.1215
You may also like:
Why Java is not 100% Object-oriented language?
What are the differences between 32-bit and 64-bit versions of Java?
Can we call static method with null object?
Can we override static method in Java?
What will be the output of following java program?
What is the difference Between java.util.Date and java.sql.Date in Java ?
What is difference between using Serializable & Externalizable Interfaces in Java?
Thanks for visiting our blog.
If you have any feedback or suggestion please feel free to drop in blow comment box.