# How to convert a Double to a number without decimals?

**URL:** <https://forums.swift.org/t/how-to-convert-a-double-to-a-number-without-decimals/52689>\
**Category:** Using Swift\
**Created:** [October 8, 2021, 5:15pm UTC](https://forums.swift.org/t/how-to-convert-a-double-to-a-number-without-decimals/52689 "2021-10-08T17:15:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luko](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/luko/32/12861_2.png) [@Luko](https://forums.swift.org/u/Luko)\
**Post date:** [October 8, 2021, 5:15pm UTC](https://forums.swift.org/t/how-to-convert-a-double-to-a-number-without-decimals/52689/1 "2021-10-08T17:15:44Z")

</div>

Hello !

I have this code

```swift
let myDouble: Double = 3.45

```

I want to keep the type as `Double`  
but I want to remove the decimals  
to get as result only **3** (no 3.45)

Are there some method to do that?

Thanks!

---

<div class="post-metadata">

**Author:** ![scanon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scanon/32/2445_2.png) [@scanon](https://forums.swift.org/u/scanon)\
**Post date:** [October 8, 2021, 5:21pm UTC](https://forums.swift.org/t/how-to-convert-a-double-to-a-number-without-decimals/52689/2 "2021-10-08T17:21:53Z")

</div>

```swift
let result = myDouble.rounded(.towardZero) // 3

```

[https://developer.apple.com/documentation/swift/floatingpoint/3017985-rounded](https://developer.apple.com/documentation/swift/floatingpoint/3017985-rounded)

---

<div class="post-metadata">

**Author:** ![Luko](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/luko/32/12861_2.png) [@Luko](https://forums.swift.org/u/Luko)\
**Post date:** [October 8, 2021, 5:35pm UTC](https://forums.swift.org/t/how-to-convert-a-double-to-a-number-without-decimals/52689/4 "2021-10-08T17:35:54Z")

</div>

> [@scanon](#):
>
> `let result = myDouble.rounded(.towardZero) // 3`

thanks @scanon !  
Do you know some way to hide the decimals?

```swift
let result = 3.45.rounded(.towardZero) // result is 3.0. I would need 3 (without decimal, without zero)

```

---

<div class="post-metadata">

**Author:** ![scanon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scanon/32/2445_2.png) [@scanon](https://forums.swift.org/u/scanon)\
**Post date:** [October 8, 2021, 6:08pm UTC](https://forums.swift.org/t/how-to-convert-a-double-to-a-number-without-decimals/52689/5 "2021-10-08T18:08:53Z")

</div>

Ah, that's a question of _formatting_, not _rounding_. The value is exactly 3, your question is just how to get it to print without any extra digits.

```swift
import Foundation

String(format: "%.0f", result)

```

---

<div class="post-metadata">

**Author:** ![Luko](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/luko/32/12861_2.png) [@Luko](https://forums.swift.org/u/Luko)\
**Post date:** [October 8, 2021, 6:10pm UTC](https://forums.swift.org/t/how-to-convert-a-double-to-a-number-without-decimals/52689/6 "2021-10-08T18:10:05Z")

</div>

Great! this is the solution

---

<div class="post-metadata">

**Author:** ![Philippe\_Hausler](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/philippe_hausler/32/22569_2.png) [@Philippe\_Hausler](https://forums.swift.org/u/Philippe_Hausler)\
**Post date:** [October 8, 2021, 6:34pm UTC](https://forums.swift.org/t/how-to-convert-a-double-to-a-number-without-decimals/52689/7 "2021-10-08T18:34:04Z")

</div>

if you want more granular control over formatting there are the new formatters in Foundation (which that `String(format:)` is also from)

```swift
result.formatted(.number.precision(.fractionLength(0)))

```

---

<div class="post-metadata">

**Author:** ![eskimo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/eskimo/32/13558_2.png) [@eskimo](https://forums.swift.org/u/eskimo)\
**Post date:** [October 11, 2021, 8:23am UTC](https://forums.swift.org/t/how-to-convert-a-double-to-a-number-without-decimals/52689/8 "2021-10-11T08:23:35Z")

</div>

> I want to remove the decimals to get as result only 3

There are various ways to do this but they divide into two groups:

- Those that produce localised results.

- Those that produce US ASCII results.

Which type of result do you want? Because:

- If you want US ASCII results and you use a localised formatter, the Arabic digits you get in some environments will probably ruin your day (-:

- Contrariwise, a user who’s expecting Arabic will not be happy with US ASCII results.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple
