LoginSignup
15
7

More than 1 year has passed since last update.

[Swift5]少数点第二位や第三位の四捨五入・切り捨て・切り上げを行う方法

Last updated at Posted at 2020-10-23

必要とする関数

四捨五入・切り捨て・切り上げを行う場合は以下の関数を使います。

round() → 四捨五入
floor() → 切り捨て
ceil() → 切り上げ

使用例

ViewController.swift
let num = 7.5

let numRound = round(num)   
// 8(四捨五入)

let numFloor = floor(num)   
// 7切り捨て)

let numCeil  = ceil(num)    
// 8(切り上げ)

少数点第二位や第三位の四捨五入・切り捨て・切り上げ

ViewController.swift
let num = 3.1415

let numRound = round(num*10)/10   
// 3.1(小数第2位で四捨五入)

let numFloor = floor(num*100)/100   
// 3.14(小数第3位で切り捨て)

let numCeil =ceil(num*1000)/1000   
// 3.142(小数第4位で切り上げ)

以上です。参考にしてください!

15
7
2

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
15
7