Monday, 3 July 2017

Format currency in Java

Sample code to format currency in java
package com.kishore.samples;

import java.text.NumberFormat;
import java.util.Locale;

/**
 * * How to format Number to different currency in Java. Following Java program
 * * will show you, how you can display double value in different currency e.g.
 * * USD, GBP and JPY. This example show price in multiple currency. 
 */
public class CurrencyFormater {
    public static void main(String args[]) {
        double price = 100.25;
        showPriceInUSD(price, getExchangeRate("USD"));
        showPriceInGBP(price, getExchangeRate("GBP"));
        showPriceInJPY(price, getExchangeRate("JPY"));
    }

    /** * Display price in US Dollar currency * * @param price * @param rate */
    public static void showPriceInUSD(double price, double rate) {
        double priceInUSD = price * rate;
        NumberFormat currencyFormat = NumberFormat
                .getCurrencyInstance(Locale.US);
        System.out.printf("Price in USD : %s %n",
                currencyFormat.format(priceInUSD));
    }

    /** * Display prince in British Pound * * @param price * @param rate */
    public static void showPriceInGBP(double price, double rate) {
        double princeInGBP = price * rate;
        NumberFormat GBP = NumberFormat.getCurrencyInstance(Locale.UK);
        System.out.printf("Price in GBP : %s %n", GBP.format(princeInGBP));
    }

    /** * Display prince in Japanese Yen * * @param price * @param rate */
    public static void showPriceInJPY(double price, double rate) {
        double princeInJPY = price * rate;
        NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.JAPAN);
        System.out.printf("Price in JPY : %s %n", currency.format(princeInJPY));
    }

    /** * @return FX exchange rate for USD * @param currency */
    public static double getExchangeRate(String currency) {
        switch (currency) {
        case "USD":
            return 1;
        case "JPY":
            return 102.53;
        case "GBP":
            return 0.60;
        case "EURO":
            return 0.73;
        default:
            throw new IllegalArgumentException(String.format(
                    "No rates available for currency %s %n", currency));
        }
    }
}

Output:

Price in USD : $100.25 
Price in GBP : £60.15 
Price in JPY : ?10,279 


Display number in different number patterns with locale

Sample code to display numbers in different pattern with locale like en_US
package com.kishore.samples;

import java.util.*;
import java.text.*;
 
public class DecimalFormatDemo {
 
   static public void customFormat(String pattern, double value ) {
      DecimalFormat myFormatter = new DecimalFormat(pattern);
      String output = myFormatter.format(value);
      System.out.println(value + " ===> " + pattern + " ===> " + output);
   }
 
   static public void localizedFormat(String pattern, double value, 
                                      Locale loc ) {
      NumberFormat nf = NumberFormat.getNumberInstance(loc);
      DecimalFormat df = (DecimalFormat)nf;
      df.applyPattern(pattern);
      String output = df.format(value);
      System.out.println(pattern + " ===> " + output + " ===> " + loc.toString());
   }
 
   static public void main(String[] args) {
       System.out.println("Number formats without Locale");
      customFormat("###,###.###", 123456.789);
      customFormat("###.##", 123456.789);
      customFormat("000000.000", 123.78);
      customFormat("$###,###.###", 12345.67);
      customFormat("\u00a5###,###.###", 12345.67);
 
      Locale currentLocale = new Locale("en", "US");
 
      DecimalFormatSymbols unusualSymbols = 
         new DecimalFormatSymbols(currentLocale);
      unusualSymbols.setDecimalSeparator('|');
      unusualSymbols.setGroupingSeparator('^');
      String strange = "#,##0.###";
      DecimalFormat weirdFormatter = new DecimalFormat(strange, unusualSymbols);
      weirdFormatter.setGroupingSize(4);
      String bizarre = weirdFormatter.format(12345.678);
      System.out.println(bizarre);
 
      Locale[] locales = {
         new Locale("en", "US"),
         new Locale("de", "DE"),
         new Locale("fr", "FR")
      };
      System.out.println("Number formats with Locale");
      for (int i = 0; i < locales.length; i++) {
         localizedFormat("###,###.###", 123456.789, locales[i]);
      }
 
   }
}

Output:

Number formats without Locale
123456.789 ===> ###,###.### ===> 123,456.789
123456.789 ===> ###.## ===> 123456.79
123.78 ===> 000000.000 ===> 000123.780
12345.67 ===> $###,###.### ===> $12,345.67
12345.67 ===> ¥###,###.### ===> ¥12,345.67
1^2345|678
Number formats with Locale
###,###.### ===> 123,456.789 ===> en_US
###,###.### ===> 123.456,789 ===> de_DE
###,###.### ===> 123 456,789 ===> fr_FR

Reference:
https://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html

Display number in different number patterns

You can use the java.text.DecimalFormat class to control the display of leading and trailing zeros, prefixes and suffixes, grouping (thousands) separators, and the decimal separator. DecimalFormat offers a great deal of flexibility in the formatting of numbers, but it can make your code more complex.
The example that follows creates a DecimalFormat object, myFormatter, by passing a pattern string to the DecimalFormat constructor. The format() method, which DecimalFormat inherits from NumberFormat, is then invoked by myFormatter—it accepts a double value as an argument and returns the formatted number in a string:
Here is a sample program that illustrates the use of DecimalFormat:
import java.text.*;

public class DecimalFormatDemo {

   static public void customFormat(String pattern, double value ) {
      DecimalFormat myFormatter = new DecimalFormat(pattern);
      String output = myFormatter.format(value);
      System.out.println(value + " ===> " + pattern + " ===> " + output);
   }

   static public void main(String[] args) {

      customFormat("###,###.###", 123456.789);
      customFormat("###.##", 123456.789);
      customFormat("000000.000", 123.78);
      customFormat("$###,###.###", 12345.67);  
   }
}

The output is:
123456.789 ===> ###,###.### ===> 123,456.789
123456.789 ===> ###.## ===> 123456.79
123.78 ===> 000000.000 ===> 000123.780
12345.67 ===> $###,###.### ===> $12,345.67

Reference:

Not able to inherit page properties from parent page

Problem:
I have created a live copy from Geometrixx site. I have pointed cq:template and sling:resourceType of parent page to my custom template and page component. I have created a new child page, when I checked the page properties of child page I don't see inheritance (lock symbol) on my child page properties dialog.

-Sample/
     -en/
          -childpage/

Solution:
I am able to see the lock symbol on child page properties dialog after adding the mixin "cq:LiveRelationship".

Saturday, 1 July 2017

AEM Page Properties Touch UI Dialog conversion issue

When tried to create a touch dialog of page properties from classic ui we may may get duplicate widgets on the newly created touch dialog from dialog conversion tool. See how to create touch dialog from dialog conversion tool.

To avoid above issue you can use granite/ui/components/foundation/include resource type in the touch ui for including granite UI components in the current component dialogs. Take a look at the following example in crxde - /libs/wcm/foundation/components/page/cq:dialog/content/items/tabs/items/basic

Failed to execute goal org.apache.felix:maven-scr-plugin

Issue:
I have created maven archetype 10 project for AEM 6.1 and jdk 1.8. When I ran mvn clean install I ran in to below errors.

1) When maven-scr-plugin (1.11.0) and org.apache.felix.scr.annotations (1.9.0)
[ERROR] Failed to execute goal org.apache.felix:maven-scr-plugin:1.11.0:scr (generate-scr-descriptor) on project digital-spearhead-core: Execution generate-scr-descriptor of goal org.apache.felix:maven-scr-plugin:1.11.0:scr failed. IllegalArgumentException -> [Help 1]

2) When maven-scr-plugin (1.12.0) and org.apache.felix.scr.annotations (1.9.0)
[ERROR] Failed to execute goal org.apache.felix:maven-scr-plugin:1.12.0:scr (generate-scr-descriptor) on project aem-sample-core: SCR Descriptor parsing had failures (see log) -> [Help 1]

Solution:
Use correct maven-scr-plugin and org.apache.felix.scr.annotations versions.  I have used maven-scr-plugin (1.20.0) and org.apache.felix.scr.annotations (1.9.0), now build is success.