Skip to main content

JSR 354 Java money api indian format

 JSR 354 – is proposed as standard api for  Currency and Money handling in java.

Here is how we can format amount to indian format as by default indian currency is formatted as US currency.

Dependency setup:
  
      javax.money
      money-api
      1.0.3
  
  
      org.javamoney
      moneta
      1.3
      pom
    


Default formatting:

 @Test
 public void defaultIndianFormat() {
     MonetaryAmount indianMoney = Monetary.getDefaultAmountFactory()
       .setCurrency("INR").setNumber(100000.12345).create();
     MonetaryAmountFormat amountFormat=MonetaryFormats.getAmountFormat(new Locale("en-IN"));
     System.out.println("Default indian money format ->"+amountFormat.format(indianMoney));
 }

Output: Default indian money format ->INR 100,000.12
 @Test
 public void testIndianFormat() {
  
  AmountFormatQuery query = AmountFormatQueryBuilder.of(new Locale("en","IN"))
    .set(AmountFormatParams.GROUPING_SIZES, new int[]{3,2}).build();
    MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(query);
    System.out.println(format);
    
    MonetaryAmount amount =
    Monetary.getDefaultAmountFactory()
    .setCurrency("INR")
    .setNumber(100000.123456).create();
    String formatted = format.format(amount);
    System.out.println("indian format output -->> "+formatted);
 }


Output: indian format output -->> INR1,00,000.12

Comments

Popular posts from this blog

Communication among micro-services

Below are good articles to read on communication among micro-services. 1.  docs@microsoft.com 2.  https://dzone.com/articles/microservices-why-asynchronous-communications 3.  microservice-design-patterns 4.  https://vertx.io/docs/vertx-amqp-bridge/java/ 5.  developers.redhat.com/blog

Using Activiti with spring mvc

Follow below steps to create web application using spring mvc and activiti in background steps :1 create maven project with following pom 4.0.0 com.gk activitiMakerChecker war 0.0.1-SNAPSHOT activitiMakerChecker Maven Webapp http://maven.apache.org 4.2.4.RELEASE 1.6 4.0.5.RELEASE 1.2 3.1.0 4.0.1.Final 3.5.6-Final org.activiti activiti-engine 5.14 org.activiti activiti-spring 5.14 org.springframework spring-beans ${org.springframework.version} org.springframework spring-context 4.2.4.RELEASE org.springframework spring-jdbc 4.2.4.RELEASE org.springframework spring-tx 4.2.4.RELEASE org.springframework spring-orm ${org.springframework.version} org.slf4j slf4j-api 1.7.2 org.hibernate hibernate-core ${org.hibernate.version} org.hibernate hibernate-entitymanager ${org.h...

Reactive programming

As per wikipedia reactive programming is declarative programming paradigm concerned with data streams and the propagation of change. Reactive programming is about having an architecture which has below four key components: Message driven  Scalable Resilient Responsive There lot of libraries/frameworks available which enable doing reactive programming across programming languages for ex in java we have RxJava, Project Reactor. Also spring 5 on wards spring framework also supports reactive programming.