4

I'm making a calculation and I'm getting 12,443. I want to round it to 12,40. This is the way I'm trying to do it but I'm getting 12 instead of 12,40

float result = Math.round( (new Float(result1)*0.3) + (new Float(result2)*0.7) );
vprosvasis.setText( Float.toString(result) );

Examples:

  • if I get 12,70001 I want to round to 12,7
  • if i get 13,4402 to round it to 13,4
  • 11,19 to 11,2

So, the final number will always be in ##.@ format

Shalom Craimer
  • 20,659
  • 8
  • 70
  • 106
kostas
  • 41
  • 1
  • 1
  • 3
  • 2
    You need to make your question clearer, and clean up the code a bit. Is your question simply, "How do I round 12.443 to 12.40"? – Robert Harvey Apr 13 '11 at 15:04
  • I dont really understand your problem. Are you rounding up decimal points too early, so your final calculation is thrown off? –  Apr 13 '11 at 15:06
  • Do you want to round to the nearest .1 then? If you're going to 2DP, then 12.44 would be the correct rounding, 12.4 at 1DP. Or do you want to go to 1DP and add an extra 0? – Phil Lello Apr 13 '11 at 15:36
  • float result=Math.round( (new Float(result1)*0.3)+(new Float(result2)*0.7)); vprosvasis.setText(Float.toString(result));} – kostas Apr 13 '11 at 15:46
  • if i use math round then it makes 12,72 to 13..but i want to make it 12,7..or,if it was 12,75 to make it 12,8..... – kostas Apr 13 '11 at 15:47
  • http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java – Robert Harvey Apr 13 '11 at 16:05
  • may i have a more clear answer please? – kostas Apr 13 '11 at 16:12
  • Since you haven't stated your question clearly it is very difficult for anyone to give you a clear answer. How many DP do you want the answer? What is your criteria for rounding? – dave.c Apr 13 '11 at 16:25
  • i have edited my question.the answer i would always like to be ##.# – kostas Apr 13 '11 at 16:31

5 Answers5

16
(float)Math.round(value * 10) / 10

should give you the result you want.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • Thanks. btw, it's Math.round() in my case (Android 4.2.2 API17). – agudoe2 Nov 10 '13 at 21:29
  • Yeah I guess it doesn't need to be more complicated than this. And if it's desired to make 12,443 to 12,40 it should be possible to just add "0" to the string where this value is to be printed – Zerato Oct 30 '17 at 12:43
2

i have this function to do it :

public static float get_ROUND(float p_VALUE, int p_DECIMAL)
{
    if (p_DECIMAL == 0) return (float) Math.round(p_VALUE);

    long l_LNG = 1;
    for (int i = 1; i <= p_DECIMAL; i++)
    {
        l_LNG = l_LNG * 10;
    }

    return (float) Math.round(p_VALUE * l_LNG) / l_LNG;
}
Johannes Mittendorfer
  • 1,102
  • 10
  • 17
Phil
  • 47
  • 1
  • 10
1
DecimalFormat  twoDForm = new DecimalFormat("#.##");
yourvalue=  Double.valueOf(twoDForm.format(yourvalue));
Tieme
  • 62,602
  • 20
  • 102
  • 156
Mudasar
  • 159
  • 1
  • 7
0
    vprosvasis.setText(""+new DecimalFormat("#,###,###.#").format((result1*0.3)+(result2*0.7)))
Peter
  • 21
  • 1
0

Use a DecimalFormat, but specify the correct rounding mode. By default it uses ROUND_HALF_EVEN, but ROUND_HALF_UP is common for a lot of financial applications.

If you really want to find and use the rounded value in further calculations (which is unusual because it produces inaccurate results), you can use BigDecimal.

I assume result1 and result2 are String instances.

float value = Float.parseFloat(result1)*0.3F + Float.parseFloat(result2)*0.7F;
BigDecimal d = new BigDecimal(String.valueOf(value));
BigDecimal rounded = d.setScale(1, BigDecimal.ROUND_HALF_EVEN);

The main advantage to using BigDecimal as opposed to multiplying, rounding, and dividing, is that you can select the rounding mode (half-up, half-even, etc.). A BigDecimal is a lot more expensive to manipulate than a primitive float, so this would be a better option for an interactive financial application than a scientific simulation.

You can then use the rounded value as is, or convert it to a float. You can convert to a String with the toString() method, but this is better done with a locale-sensitive DecimalFormat object, which will accept BigDecimal with its format() method.

erickson
  • 265,237
  • 58
  • 395
  • 493