Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;

import java.util.Locale;

public class Main {
    public static String roundWithTwoDecimals(double number) {
        try {
            // format based on default locale
            DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.US);
            DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
            df.setDecimalFormatSymbols(dfs);
            df.applyPattern("#####0.00");
            return df.format(number);
        } catch (IllegalArgumentException e) {
            return null;
        }
    }
}