1. Code:
Import Java.math.BigDecimal;
Import Java.text.DecimalFormat;
Import Java.text.NumberFormat;
public class Format {
double f = 111231.5585;
public void M1 () {
BigDecimal bg = new BigDecimal (f);
Double f1 = Bg.setscale (2, bigdecimal.round_half_up). Doublevalue ();
System.out.println (F1);
}
/**
* DecimalFormat conversion is the simplest/public
void m2 () {
DecimalFormat df = new DecimalFormat ("#.00");
System.out.println (Df.format (f));
}
/**
* String.Format printing is easiest/public
void m3 () {
System.out.println (String.Format ("%.2f", f));
} Public
void M4 () {
NumberFormat nf = numberformat.getnumberinstance ();
Nf.setmaximumfractiondigits (2);
System.out.println (Nf.format (f));
}
public static void Main (string[] args {
format f = new format ();
F.M1 ();
F.M2 ();
F.M3 ();
F.M4 ();
}
2. Output Result:
111231.56
111231.56
111231.56
111,231.56
Here we provide a tool class that defines the addition, subtraction, multiplication, addition, and rounding methods of floating-point numbers. For reference.
Source file Mathextend.java:
Import Java.math.BigDecimal;
public class Mathextend {//default division arithmetic precision private static final int default_div_scale = 10;
/** * Provides an accurate addition operation. * @param v1 * @param v2 * @return two parameters and/or public static double add (double v1, double v2) {BigDecimal B1 =
New BigDecimal (double.tostring (v1));
BigDecimal b2 = new BigDecimal (double.tostring (v2));
Return B1.add (B2). Doublevalue (); /** * provides exact addition operations * @param v1 * @param v2 * @return two parameters mathematically Plus, return in string format/public static string Add (Stri
NG v1, String v2) {BigDecimal B1 = new BigDecimal (v1);
BigDecimal b2 = new BigDecimal (v2);
Return B1.add (B2). toString ();
/** * Provides an accurate subtraction operation. * @param v1 * @param v2 * @return Two parameters of the difference */public static double subtract (double v1, double v2) {BigDecimal
B1 = new BigDecimal (double.tostring (v1));
BigDecimal b2 = new BigDecimal (double.tostring (v2));
Return B1.subtract (B2). Doublevalue ();
/** * provides accurate subtraction operations * @param v1
* @param v2 * @return Two parameters are mathematically poor, returned in string format */public static string subtract (String v1, String v2) {Bigdecima
L B1 = new BigDecimal (v1);
BigDecimal b2 = new BigDecimal (v2);
Return B1.subtract (B2). toString ();
/** * Provides an accurate multiplication operation. * @param v1 * @param v2 * @return Two parameters of the product */public static double multiply (double v1, double v2) {BigDecimal
B1 = new BigDecimal (double.tostring (v1));
BigDecimal b2 = new BigDecimal (double.tostring (v2));
Return b1.multiply (B2). Doublevalue (); /** * provides accurate multiplication operations * @param v1 * @param v2 * @return The mathematical product of two parameters, returning in string format/public static string Multip
Ly (String v1, String v2) {BigDecimal B1 = new BigDecimal (v1);
BigDecimal b2 = new BigDecimal (v2);
Return b1.multiply (B2). toString (); /** * provides (relative) accurate division operations, when in addition to the situation, accurate to the * decimal point after 10 digits, after the number rounding, rounding mode using Round_half_even * @param v1 * * @param v2 *
Return two parameters of the merchant/public static double divide (double v1, double v2) {Return Divide (v1, v2, Default_div_scale); /** * provides (relative) precise division operations. When it occurs, the scale parameter refers to the fixed precision, and the subsequent digits are rounded up.
The rounding mode uses the Round_half_even * @param v1 * @param v2 * @param scale indicates the need to be accurate to several decimal places. * @return Two-parameter quotient/public static double divide (double v1,double v2, int scale) {return divide (V1, V2, scale,
Bigdecimal.round_half_even); /** * provides (relative) precise division operations. When it occurs, the scale parameter refers to the fixed precision, and the subsequent digits are rounded up. The rounding mode takes the user-specified rounding mode * @param v1 * @param v2 * @param scale indicates the need to be accurate to several * @param after the decimal point Round_mode represents the user-specified rounding mode * @ret
Urn two parameters of the quotient/public static double divide (double v1,double v2,int scale, int round_mode) {if (Scale < 0)
{throw new IllegalArgumentException ("The scale must be a positive integer or zero");
} BigDecimal B1 = new BigDecimal (double.tostring (v1));
BigDecimal b2 = new BigDecimal (double.tostring (v2));
Return B1.divide (B2, scale, Round_mode). Doublevalue ();
/** * provides (relative) accurate division operations, which, when occurring in addition to the circumstances, are accurate to
* After the decimal point 10 digits, after the number is rounded, the rounding pattern uses Round_half_even * @param v1 * @param v2 * @return Two parameter's quotient, returns in the string form * * * Public St
Atic string Divide (String v1, String v2) {return divide (V1, v2, Default_div_scale); /** * provides (relative) precise division operations. When it occurs, the scale parameter refers to the fixed precision, and the subsequent digits are rounded up.
The rounding mode uses the Round_half_even * @param v1 * @param v2 * @param scale represents the quotient that needs to be accurate to several * @return two parameters after the decimal point, return in string format * * public static string divide (String v1, string v2, int scale) {return divide (V1, v2, Default_div_scale, BIGDECIMAL.R
Ound_half_even); /** * provides (relative) precise division operations. When it occurs, the scale parameter refers to the fixed precision, and the subsequent digits are rounded up. The rounding mode takes the user-specified rounding mode * @param v1 * @param v2 * @param scale indicates the need to be accurate to several * @param after the decimal point Round_mode represents the user-specified rounding mode * @ret Urn two arguments, return in string format/public static string divide (String v1, string v2, int scale, int round_mode) {if (scale
< 0) {throw new IllegalArgumentException ("The scale must be a positive integer or zero"); } BigDecimal B1 = new BIGDEcimal (v1);
BigDecimal b2 = new BigDecimal (v2);
Return B1.divide (B2, scale, Round_mode). toString (); /** * Provides accurate decimal rounding, rounding mode with Round_half_even * @param v need to be rounded number * @param scale decimal point after a few * @return rounded
The result * * public static double round (double V,int scale) {return round (V, scale, bigdecimal.round_half_even); /** * Provides accurate decimal rounding processing * @param v needs rounded digits * @param scale decimal point retains several * @param round_mode specified rounding mode * @return Rounded result * * public static double round (double v, int scale, int round_mode) {if (scale<0) {throw
New IllegalArgumentException ("The scale must be a positive integer or zero");
} BigDecimal B = new BigDecimal (double.tostring (v));
Return B.setscale (scale, Round_mode). Doublevalue (); /** * Provides accurate decimal rounding, rounding mode with Round_half_even * @param v need to be rounded number * @param scale decimal point after a few * @return rounded The result is returned in string format */public static string round (string v, int scale) {returnRound (V, scale, bigdecimal.round_half_even); /** * Provides accurate decimal rounding processing * @param v needs rounded digits * @param scale decimal point retains several * @param round_mode specified rounding mode * @return
Rounded result, returned in string format/public static string round (string v, int scale, int round_mode) {if (scale<0) {
throw new IllegalArgumentException ("The scale must be a positive integer or zero");
} BigDecimal B = new BigDecimal (v);
Return B.setscale (scale, Round_mode). toString (); }
}