Format price To String - Android java.lang

Android examples for java.lang:String Format

Description

Format price To String

Demo Code

/**/*from  ww w  . j a  v  a2  s .  c om*/
 * (c) Winterwell Associates Ltd, used under MIT License. This file is background IP.
 */
//package com.java2s;

public class Main {
    /**
     * 
     * @param pounds e.g. "?"
     * @param pence e.g. "p". Can be null, in which case the pounds symbol is
     *            always used.
     * @param price
     * @return e.g. "?1.50", "50p", "free"
     */
    public static String priceToString(String pounds, String pence,
            int price) {
        assert price >= 0 : price;
        assert pounds != null;
        if (price == 0)
            return "free";
        int numPounds = price / 100;
        int numPence = price % 100;
        if (price < 100 && pence != null)
            return numPence + pence;

        return pounds + String.format("%d.%02d", numPounds, numPence);
    }
}

Related Tutorials