-1

I'm rounding off some value when I passed these values in my round() func, in my round func I used Math.pow and Math.round to achieve:

round(x,2);

round(xx,2);

float x =4.68;
float xx = 4.00;

after my round off function

public static Float round(Float value, int places) {
        if (value != null) {
            Float scale = (float) Math.pow(10, places);
            return (Float) (Math.round(value * scale) / scale);
        }
        return value;
    }
Output Coming:
x = 4.7
x = 4

expected Output is or REQUIREMENT IS:
x-->4.70
x-->4.00
theforestecologist
  • 3,778
  • 2
  • 44
  • 79
Parminder Singh
  • 111
  • 3
  • 9

1 Answers1

1

If you call function round(x,2) it means you expect as an output a number rounded to 2 decimal places. It's doesn't matter if it is 4.7 or 4.70000. This is still the same number.

I think your issue is about formatting your output for printing. In this case you can use simple code like this:

    float x =4.68;
    float xx = 4.00;
    System.out.printf("%.2f", x);
    System.out.printf("%.2f", xx);
dgebert
  • 944
  • 1
  • 10
  • 21
  • i need the value only in float type this System.out.printf("%.2f", x); is converting into String – Parminder Singh Aug 30 '18 at 14:17
  • @ParminderSingh Within a `float` variable, `4.7` and `4.70` are identical. Why do you need the unnecessary 0? – achAmháin Aug 30 '18 at 15:16
  • @ParminderSingh You have to understand that 4.7 is the same as 4.70, 4.700 etc. If you see value on your screen, it is a String representation of this float. So all you can do is to manipulate with printing method. – dgebert Aug 30 '18 at 20:10