#rounding numbers

42 messages · Page 1 of 1 (latest)

real holly
#

heyy i want a user to input a number as an int and then inputted it rounded ex: user-34 output-30 😀

clever spruce
#

What does "rounded" mean to you?

#

Why would 34 be converted to 30?

exotic pasture
#

if you just want to round to the nearest 10, you can just use a ternary

#
int rounded = (unrounded % 10 < 5)
  ? (unrounded - (unrounded % 10))
  : (unrounded + (10 - unrounded % 10))
clever spruce
#

That doesn't look simple to me, but I guess I wouldn't try to do much for someone who ignores the easiest and most obvious question

raw zenith
#

Math.round(number/10)*10

clever spruce
#

I think you misread something

raw zenith
#

34 rounded to the nearest ten is 30 I guess. based on what they said that code should work.

clever spruce
#

If I'm not wrong, 34*10 is 340 and rounding it make it stay 340

raw zenith
#

omgg sry

#

now it should be okay

real holly
raw zenith
real holly
#

you sure?

raw zenith
#

have u tried...

real holly
#

ty

#

!

raw zenith
#

np

real holly
#

appreciate

#

that!

#

but i want it if its <=5

#

to be rounded up

#

@raw zenith

raw zenith
#

should still work?

#

./run java System.out.println(Math.round(5d/10)*10);

vestal oarBOT
#

Here is your java(15.0.2) output @raw zenith

10
raw zenith
#

hmm

#

make sure it's a double

#

then it should work

real holly
#

how?

#

is that working?

#

in a double?

raw zenith
#

yes

#

multiply to a double

#

e.g. 10.0 10d or 10D

raw zenith
#

@real holly has this issue been resolved?

#

is so please do /unreserve

real holly
#

yes!