1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Swift】 絶対値に変換する方法【初心者】

Last updated at Posted at 2020-12-06

#はじめに
絶対値にする方法がわからなかったので、調べた事をメモとして記録しておこうと思います。

#環境
macOS Catalina バージョン10.15.4
Xcode バージョン11.7
Swift5

###絶対値にする(Int型)
absを使用

//Int型
 let numberA:Int = -1
 print(numberA)//-1
 //絶対値にする
 print(abs(numberA))//1

###絶対値にする(Double型)
fabsを使用

//Double型
let numberB:Double = -1.0
print(numberB)//-1.0
//絶対値にする
print(fabs(numberB))//1.0

###絶対値にする(Float型)
fabsfを使用

 //Float型
let numberC:Float = -1.0
print(numberC)//-1.0
//絶対値にする
print(fabsf(numberC))//1.0

#全コード

import UIKit

class ViewController: UIViewController {

    //Int型
    let numberA:Int = -1
    //Double型
    let numberB:Double = -1.0
    //Float型
    let numberC:Float = -1.0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        print(numberA)//-1
        //絶対値にする
        print(abs(numberA))//1
        
        print(numberB)//-1.0
        //絶対値にする
        print(fabs(numberB))//1.0
        
        print(numberC)//-1.0
        //絶対値にする
        print(fabsf(numberC))//1.0
        
    }
}

#終わりに
初学者のため、もし間違いがあれば教えていただければ幸いです。

twitter

1
2
4

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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?