## Round Double to closest 10

You can use the `round()`

function (which rounds a floating point number

to the nearest integral value) and apply a "scale factor" of 10:

`func roundToTens(x : Double) -> Int {`

return 10 * Int(round(x / 10.0))

}

Example usage:

`print(roundToTens(4.9)) // 0`

print(roundToTens(15.1)) // 20

In the second example, `15.1`

is divided by ten (`1.51`

), rounded (`2.0`

),

converted to an integer (`2`

) and multiplied by 10 again (`20`

).

**Swift 3:**

`func roundToTens(_ x : Double) -> Int {`

return 10 * Int((x / 10.0).rounded())

}

Alternatively:

`func roundToTens(_ x : Double) -> Int {`

return 10 * lrint(x / 10.0)

}

## How can I round Int to nearest 10 in Swift?

You need to use round function and `x % 5 == 0`

check.

`let values = (6...100).map({ Double($0) })`

func round(_ value: Double, toNearest: Double) -> Double {

return round(value / toNearest) * toNearest

}

for x in values {

if x.truncatingRemainder(dividingBy: 5) == 0 {

print("x - \(x), y - \(Int(x / 5))")

} else {

let rounded = round(x, toNearest: 10.0)

print("x - \(x), y - \(Int(rounded / 5))")

}

}

## How to round up to nearest 10 or 100 or x

Please try this formula

`System.out.println(Math.ceil(x*Math.pow(10, -Math.floor(Math.log10(x)))) / Math.pow(10, -Math.floor(Math.log10(x))) ) ;`

You can try this formula online.

`class Main {`

public static void niceround(double x) {

System.out.println(x+" to " + Math.ceil(x * Math.pow(10, -Math.floor(Math.log10(x)))) / Math.pow(10, -Math.floor(Math.log10(x))));

}

public static void main(String[] args) {

niceround(0.0322);

niceround(3.22);

niceround(32.2);

niceround(42.2);

niceround(422.2);

}

}

test

`/usr/lib/jvm/java-1.8.0-openjdk-amd64/bin/java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:36808,suspend=y,server=n -Dfile.encoding=UTF-8 -classpath /usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/charsets.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/cldrdata.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/icedtea-sound.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/jaccess.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/nashorn.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/sunec.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/zipfs.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/jce.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/jsse.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/management-agent.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/resources.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/rt.jar:/home/dac/proj/javatest2016/out/production/javatest2016:/home/dac/Downloads/idea-IU-145.972.3/lib/idea_rt.jar Main`

Connected to the target VM, address: '127.0.0.1:36808', transport: 'socket'

0.0322 to 0.04

3.22 to 4.0

32.2 to 40.0

42.2 to 50.0

422.2 to 500.0

Disconnected from the target VM, address: '127.0.0.1:36808', transport: 'socket'

Process finished with exit code 0

## How to round a double to the nearest 10th place

In regards to your answer to yourself, you don't want to convert an object of type A to type B, operate on it, and then convert it back to type A. Not if you can do anything else.

The following does what you want it to do:

`public double hypotenuse()`

{

return Math.round(Math.hypot(leg, leg) * 10) / 10.0;

}

I'm not entirely sure why. It would take some digging into what this compiles down to at the assembly level.

The *real problem* is that each double in Java only has a limited number of bits to store its decimal portion. There are an infinite number of possible real numbers in real life. There is no way you can represent every single real number with only 64 bits. This is why you should avoid floating point calculations in banking apps and software that requires the number to be exact. Multiplying a few doubles together can introduce quite a significant error margin in the calculation. See this explanation on another SO answer, which does a great job of explaining this further.

If you really need it to be exact, use `BigDecimal`

.

## How to round a Double to the nearest Int in swift?

There is a `round`

available in the `Foundation`

library *(it's actually in Darwin, but Foundation imports Darwin and most of the time you'll want to use Foundation instead of using Darwin directly)*.

`import Foundation`

users = round(users)

Running your code in a playground and then calling:

`print(round(users))`

Outputs:

15.0

`round()`

always rounds up when the decimal place is `>= .5`

and down when it's `< .5`

(standard rounding). You can use `floor()`

to force rounding down, and `ceil()`

to force rounding up.

If you need to round to a specific place, then you multiply by `pow(10.0, number of places)`

, `round`

, and then divide by `pow(10, number of places)`

:

Round to 2 decimal places:

`let numberOfPlaces = 2.0`

let multiplier = pow(10.0, numberOfPlaces)

let num = 10.12345

let rounded = round(num * multiplier) / multiplier

print(rounded)

Outputs:

10.12

**Note:** Due to the way floating point math works, `rounded`

may not always be perfectly accurate. It's best to think of it more of an approximation of rounding. If you're doing this for display purposes, it's better to use string formatting to format the number rather than using math to round it.

## How can I round a number down to the nearest 10?

You could use TRUNCATE function as follow:

`SELECT TRUNCATE(819, -1);`

SELECT TRUNCATE(812, -1);

Result:

810

## Rounding integers to nearest multiple of 10

I would just create a couple methods;

`int RoundUp(int toRound)`

{

if (toRound % 10 == 0) return toRound;

return (10 - toRound % 10) + toRound;

}

int RoundDown(int toRound)

{

return toRound - toRound % 10;

}

Modulus gives us the remainder, in the case of rounding up `10 - r`

takes you to the nearest tenth, to round down you just subtract r. Pretty straight forward.

## How to round up a number to nearest 10?

`floor()`

will go down.

`ceil()`

will go up.

`round()`

will go to nearest by default.

Divide by 10, do the ceil, then multiply by 10 to reduce the significant digits.

`$number = ceil($input / 10) * 10;`

Edit: I've been doing it this way for so long.. but TallGreenTree's answer is cleaner.

## round decimal to nearest 10th

Change a bit the pattern to hard-code the final zero:

`double finalPrice = 2.46;`

DecimalFormat fmt = new DecimalFormat("0.0'0'");

System.out.println("£" + fmt.format(finalPrice) + " Approx");

Now, if you're manipulating real-world money, you'd better not use double, but int or BigInteger.

### Related Topics

How to Detect Which Skspritenode Has Been Touched

Iterate Over Collection Two At a Time in Swift

First Item in a List Is Always Selected

Print Without Newline in Swift

Dispatch_Once After the Swift 3 Gcd API Changes

Providing a Default Value For an Optional in Swift

How to Atomically Increment a Variable in Swift

How to Convert a Decimal Number to Binary in Swift

Nsattributedstring, Change the Font Overall But Keep All Other Attributes

Class Conforming to Protocol as Function Parameter in Swift

How to Create a Multi-Line Text Inside a Scrollview in Swiftui

Tabview Resets Navigation Stack When Switching Tabs

Computed Read-Only Property VS Function in Swift

How to Check If a Text Field Is Empty or Not in Swift