Can anyone help me to get the estimate value of float value 1001.27124 to 1001.27? Math.round(float) function return the int value. But I want to get float value result with 2 decimal point; Thanks in advance.

Dani AI

Generated

A few good answers are already here, but it helps to separate two goals: (1) round the numeric value for further math, vs (2) format for display. NPH’s multiply-and-round works for many values, but because binary floating point cannot represent most decimals exactly, tricky cases like 2.675 may print as 2.67 instead of 2.68. For calculations (especially money), avoid float/double and round with BigDecimal.

Example: round the value itself to 2 decimals.

import java.math.BigDecimal;
import java.math.RoundingMode;

double raw = 1001.27124;

// Prefer valueOf or a String to avoid binary->decimal surprises
BigDecimal bd = BigDecimal.valueOf(raw).setScale(2, RoundingMode.HALF_UP);

// Use bd for further math, or convert back if you must:
double rounded = bd.doubleValue();    // 1001.27

Notes:

  • RoundingMode.HALF_UP matches everyday expectations; for finance, consider RoundingMode.HALF_EVEN (bankers’ rounding). The old BigDecimal.ROUND_* constants used by cheenu78 are deprecated; use RoundingMode instead.
  • Avoid new BigDecimal(double); use BigDecimal.valueOf(double) or new BigDecimal("...").

For display only, build on jwenting’s point and fix both min and max fraction digits so you always see two decimals (server_crash’s "###.##" can drop trailing zeros):

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

double num = 1001.27124;

NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(2);

String out = nf.format(num);          // "1001.27"
System.out.println(out);

Tip: use Locale.US (or your target locale) to control the decimal separator and grouping. Use BigDecimal for the math, NumberFormat/printf for the UI.

Recommended Answers

All 7 Replies

Try this code

double num = 1001.27124;
    	
double newNum = Math.round(num*100.0)/100.0;
System.out.println (newNum);  //prints 1001.27

It works because *100 shifts the numbers to the left twice. Then the round is applied. Then we divide by 100 to shift twice right.

For more help,

import java.util.*;

DecimalFormat dec = new DecimalFormat("###.##");

System.out.println(dec.format(value));

better to use the setMinimumFractionDigits and setMaximumFractionDigits functions on DecimalFormat.

The format strings can be confusing :)

hi,
i believe you can try this program...

import java.math.*;
class TestBigDecimal
{
	public static void main(String args[])
	{
		BigDecimal bd=new BigDecimal(args[0]);
		bd=bd.setScale(2,BigDecimal.ROUND_HALF_EVEN);
		System.out.println(bd);
	}
}

Srinivas

Just in case if someone need to format a double number to some specific fraction length.

There is a class in java.text package called NumberFormat. You can use .setMaximumFractionDigits(fraction) method to set the maximum fraction for double. Then use format(doublevalue) method which returns string with the defined fraction length value. There are more methods which can be useful for NumberFormatting.

NumberFormat nf = NumberFormat.getInstance();
            nf.setMaximumFractionDigits(2);            
            nf.setGroupingUsed(false); 
            // Setting this to true will give you xx,xxx,xxx type of formatting.
            String formattedvalue = nf.format(adoublevalue);
double num = 1001.27124;
System.out.printf("%8.2f\n", num);

import java.text.*;

DecimalFormat dec = new DecimalFormat("###.##");

System.out.println(dec.format(value));

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.