1

for example i have a result of circle area = 4.3, my question is 'when i get number grater than .5 [for example(4.6, 4.7, 4.8, 4.9)], i want it to give me 5.0, but if less than .5 [4.4, 4.3, 4.2, 4.1] then show me 4.0'

i tried Math.round() but it converts float to int like [4], but i don't want to convert it to int

is there a way to do this?

here is my code:

private float radius;
private double result;

public void show(float x)
{
    this.radius = x;
    this.result=Math.pow(x,2)*Math.PI;

    System.out.println("result is: "+result);
}

clearly, i want both method(Math.floor()) and (Math.ceil()) in one function thanks

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
  • 3
    `Math.round(4.5f)` will return 5 and do exactly what you want. And converting an int to a float/double really isn't that hard at all. – OH GOD SPIDERS Jan 15 '18 at 12:31
  • Alternatively, since you know the area will always be positive, you could use `Math.floor( area + .5 )`. – Thomas Jan 15 '18 at 12:33
  • 1
    `(float)Math.round(x)` makes the int again a float. However `float` and `double` are an approximation (of sums of powers of 2), without fixed number of decimals, and 5.3 has in reality a small error, 10000*5.3 will not be exactly 53000. – Joop Eggen Jan 15 '18 at 12:34
  • @OHGODSPIDERS thanks, =) – mhamad arsalan Jan 15 '18 at 12:40
  • [Rounding a double to turn it into an int (java)](https://stackoverflow.com/q/2654839) – Bernhard Barker Jan 15 '18 at 12:41
  • @Thomas wow, this is exactly what i want! thank you very much – mhamad arsalan Jan 15 '18 at 12:44
  • @Dukeling yes it works, but i don't want to convert float to int, i want to keep the float point, like(if the point >.5, floor it, and if the point <.5, ceil it) thanks – mhamad arsalan Jan 15 '18 at 12:49
  • @JoopEggen thanks, it works =), but for ("10000*5.3") , I tried windows calculator and it gives me exactly 53000! i don't know if windows has it's own exception handler – mhamad arsalan Jan 15 '18 at 12:56
  • 1
    The Windows calculator internally does not use float/double, but something like what is BigDecimal in java. – Joop Eggen Jan 15 '18 at 13:03
  • @Dukeling thanks :) – mhamad arsalan Jan 15 '18 at 13:10
  • @JoopEggen it's strange to me, i mean i'm beginner of codding in java i absolutely have no idea about what you mean by ("windows calculator internally does not use float/double")(sorry for that) or maybe because my English is bad(i don't understand too much English) . sorry, thanks :) – mhamad arsalan Jan 15 '18 at 13:16
  • To avoid be able to calculate more precise, with a precision after the decimal point, java has the class BigDecimal. There `12.01 * 4.0` is exactly `48.040`. (BTW only about 10% of the world are good English speakers, and the number is slowly diminishing.) – Joop Eggen Jan 15 '18 at 14:04
  • @JoopEggen thank you for your help, i think i'm understand – mhamad arsalan Jan 16 '18 at 05:40

1 Answers1

0

Math.rint is probably closest to what you're looking for. Note that it rounds half to even.

Simon Byrne
  • 7,694
  • 1
  • 26
  • 50