LoginSignup
8
5

More than 5 years have passed since last update.

[Swift] Double(Float)型の数値を整数部と小数部で分けて取り出す

Last updated at Posted at 2019-04-03

10.125100.125に分けて取り出したい。

let value = 10.125
let integer = Int(value)
let fraction = value.truncatingRemainder(dividingBy: 1)

print(integer)  // 10
print(fraction) // 0.125

value.truncatingRemainder(dividingBy: 1)value % 1と同じで1で割った余りの数を計算する。

8
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
5