Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.text.NumberFormat;

import java.util.Locale;

public class Main {
    public static String formatMoneyAmountItemised(double amount, Locale locale) {

        NumberFormat format = NumberFormat.getCurrencyInstance(locale);

        format.setMinimumFractionDigits(0);
        format.setMaximumFractionDigits(3);

        String amountTxt = format.format(amount);

        String amountTxtValue = "";
        for (int i = 0; i < amountTxt.length(); i++) {
            if (Character.isDigit(amountTxt.charAt(i)) || amountTxt.charAt(i) == '.' || amountTxt.charAt(i) == ',')
                amountTxtValue = amountTxtValue + amountTxt.charAt(i);
        }

        amountTxt = amountTxtValue;

        if (amountTxt.endsWith(",0"))
            return (amountTxt.replace(",0", ""));
        else if (amountTxt.endsWith(",00"))
            return (amountTxt.replace(",00", ""));
        else if (amountTxt.endsWith(",000"))
            return (amountTxt.replace(",000", ""));
        else
            return (amountTxt);

    }
}