Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Decimal、正しく使ってる? / Are you using Decimal correc...

Elvis Shi
December 15, 2021

Decimal、正しく使ってる? / Are you using Decimal correctly?

Elvis Shi

December 15, 2021
Tweet

More Decks by Elvis Shi

Other Decks in Programming

Transcript

  1. } var employedBy = "YUMEMI Inc." var job = "iOS

    Tech Lead" var favoriteLanguage = "Swift" var twitter = "@lovee" var qiita = "lovee" var github = "el-hoshino" var additionalInfo = """ M1 Max MacBook Proࠓ೔ಧ͍ͨʂ """ final class Me: Developable, Talkable {
  2. // ࠓ݄ͷڅ༩ var myYumeCoin = 0.2 // ࠓ݄ͷϘʔφε myYumeCoin +=

    0.1 // ϘʔφεΛ࢖ͬͯങ͍෺ʂ myYumeCoin -= 0.1 // ࢒ߴ֬ೝ print(myYumeCoin) // 0.20000000000000004 ??????
  3. // ࠓ݄ͷڅ༩ var myYumeCoin = 0.2 // ࠓ݄ͷϘʔφε myYumeCoin +=

    0.1 // ϘʔφεΛ࢖ͬͯങ͍෺ʂ myYumeCoin -= 0.1 // ࢒ߴ֬ೝ print(myYumeCoin) // 0.20000000000000004 ??????
  4. import Foundation // ࠓ݄ͷڅ༩ var myYumeCoin: Decimal = 0.2 //

    ࠓ݄ͷϘʔφε myYumeCoin += 0.1 // ϘʔφεΛ࢖ͬͯങ͍෺ʂ myYumeCoin -= 0.1 // ࢒ߴ֬ೝ print(myYumeCoin) // 0.2 🎉 'PVOEBUJPO͕ඞཁ ܕΛ໌ࣔతʹ%FDJNBMʹࢦఆ
  5. import Foundation // ࠓ݄ͷڅ༩ var myYumeCoin: Decimal = 0.2 //

    ࠓ݄ͷϘʔφε myYumeCoin += 0.1 // ϘʔφεΛ࢖ͬͯങ͍෺ʂ myYumeCoin -= 0.1 // ࢒ߴ֬ೝ print(myYumeCoin) // 0.2 🎉
  6. import Foundation // ࠓ݄ͷڅ༩ var myYumeCoin: Decimal = 0.2 //

    ࠓ݄ͷϘʔφε myYumeCoin += 0.1 // ϘʔφεΛ࢖ͬͯങ͍෺ʂ myYumeCoin -= 0.1 // Decimal Λ࢖ͬͯਖ਼֬ͳڅ༩ܭࢉ͕Ͱ͖͔ͨΒ͍͑΍Ϙʔφεʂ myYumeCoin += 123.456 // ࢒ߴ֬ೝ print(myYumeCoin) // 123.65599999999997952 !!!?????
  7. %FDJNBMΛखܰʢʁʣʹѻ͏ͨΊͷ 
 ΠχγϟϥΠβʔ • init(_ value: String, format: Decimal.FormatStyle, lenient:

    Bool = true) throws // iOS 15+ • init?(string: String, locale: Locale? = nil) // iOS 7+ • // etc…
  8. import Foundation // ࠓ݄ͷڅ༩ var myYumeCoin: Decimal = 0.2 //

    ࠓ݄ͷϘʔφε myYumeCoin += 0.1 // ϘʔφεΛ࢖ͬͯങ͍෺ʂ myYumeCoin -= 0.1 // Decimal Λ࢖ͬͯਖ਼֬ͳڅ༩ܭࢉ͕Ͱ͖͔ͨΒ͍͑΍Ϙʔφεʂ myYumeCoin += 123.456 // ࢒ߴ֬ೝ print(myYumeCoin) // 123.65599999999997952 !!!?????
  9. import Foundation // … // Decimal Λ࢖ͬͯਖ਼֬ͳڅ༩ܭࢉ͕Ͱ͖͔ͨΒ͍͑΍Ϙʔφεʂ //myYumeCoin += 123.456

    // ࠓ౓ͦ͜ʂ myYumeCoin += try! .init("123.456", format: .number) // ࢒ߴ֬ೝ print(myYumeCoin) // 123.656 🎉