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

数値を文字列に整形する際の落とし穴とその解決策(iOSDC2024 ルーキーズLT) / iO...

数値を文字列に整形する際の落とし穴とその解決策(iOSDC2024 ルーキーズLT) / iOSDC Japan 2024 Formatting Floating-Point Numbers

数値を文字列に整形する際に陥った落とし穴について、具体例を交えて紹介します。

glassfiber

August 23, 2024
Tweet

More Decks by glassfiber

Other Decks in Programming

Transcript

  1. ࢖༻཰Λύʔηϯτදࣔ͢Δ • খ਺ୈ2Ґ·Ͱදࣔ͢ΔίʔυΛॻ͖·ͨ͠ • ҰݟΑͦ͞͏ʹݟ͑·͢ ͕ 00 let usageRate =

    0.12345 let value = round(usageRate * 10000) / 10000 let percentValue = value * 100 print("\(percentValue)%") 12.35% ↑0.01%=0.0001ͳͷͰ ɹখ਺ୈ5ҐͰ࢛ࣺޒೖ͢Δ
  2. ਺஋ͷ੔ܗͱ͍͑͹NumberFormatter • numberStyle = .percent ͱ͢Ε͹100ഒͤͣͱ΋ύʔηϯτදهʹͳΔ • ͢͹Β͍͠ 00 let

    usageRate = 0.523 let formatter = NumberFormatter() formatter.numberStyle = .percent formatter.maximumFractionDigits = 2 if let str = formatter.string(from: NSNumber(value: usageRate)) { print(str) } 52.3% ←খ਺఺ҎԼ࠷େ2ܻ
  3. খ਺఺ҎԼ2ܻʹͰ͖ͨɺ͚Ͳ • ཁ๬͕͖ͨ • 12.34%ͱ͍͏ͷ͸௕͍ɺ੔਺Ͱे෼ • Ͱ΋0.12%͸͜ͷ··খ਺఺ҎԼ2ܻग़ͯཉ͍͠ • →஋ʹΑܻͬͯ਺Λ͍͍ײ͡ʹ੾Γସ͍͑ͨ 00

    let formatter = NumberFormatter() formatter.numberStyle = .percent func myFormat(_ value: Double) -> String { formatter.maximumFractionDigits = 2 return formatter.string(from: NSNumber(value: value)) ?? "" } print(myFormat(0.12345)) print(myFormat(0.012345)) print(myFormat(0.0012345)) 12.34% 1.23% 0.12%
  4. Ͱ͸ɺܻ਺Λ੾Γସ͑ͯΈΔ 00 let formatter = NumberFormatter() formatter.numberStyle = .percent func

    myFormat(_ value: Double) -> String { formatter.maximumFractionDigits = switch value { case ..<1: 2 case ..<10: 1 default: 0 } return formatter.string(from: NSNumber(value: value)) ?? "" } print(myFormat(0.12345)) print(myFormat(0.012345)) print(myFormat(0.0012345)) ←஋ͷൣғͰ৔߹෼͚ܻͯ͠਺Λ੾Γସ͑Δ 1%ະຬ / 1%Ҏ্10%ະຬ / 10%Ҏ্
  5. Ͱ͸ɺܻ਺Λ੾Γସ͑ͯΈΔ 00 let formatter = NumberFormatter() formatter.numberStyle = .percent func

    myFormat(_ value: Double) -> String { formatter.maximumFractionDigits = switch value { case ..<1: 2 case ..<10: 1 default: 0 } return formatter.string(from: NSNumber(value: value)) ?? "" } print(myFormat(0.12345)) print(myFormat(0.012345)) print(myFormat(0.0012345)) ←஋ͷൣғͰ৔߹෼͚ܻͯ͠਺Λ੾Γସ͑Δ 1%ະຬ / 1%Ҏ্10%ະຬ / 10%Ҏ্ 12.34% 1.23% 0.12% มΘͬͯͳ͍…ʁ
  6. Ͱ͸ɺܻ਺Λ੾Γସ͑ͯΈΔ 00 let formatter = NumberFormatter() formatter.numberStyle = .percent func

    myFormat(_ value: Double) -> String { formatter.maximumFractionDigits = switch value { case ..<1: 2 case ..<10: 1 default: 0 } return formatter.string(from: NSNumber(value: value)) ?? "" } print(myFormat(0.12345)) print(myFormat(0.012345)) print(myFormat(0.0012345)) 12.34% 1.23% 0.12% มΘͬͯͳ͍…ʁ ←ˋͷ਺஋Ͱൺֱ͢Δؒҧ͍ (Α͘΍Δ)
  7. Ͱ͸ɺܻ਺Λ੾Γସ͑ͯΈΔ 00 let formatter = NumberFormatter() formatter.numberStyle = .percent func

    myFormat(_ value: Double) -> String { formatter.maximumFractionDigits = switch value { case ..<0.01: 2 case ..<0.1: 1 default: 0 } return formatter.string(from: NSNumber(value: value)) ?? "" } print(myFormat(0.12345)) print(myFormat(0.012345)) print(myFormat(0.0012345)) ←஋ͷൣғͰ৔߹෼͚ܻͯ͠਺Λ੾Γସ͑Δ 1%ະຬ / 1%Ҏ্10%ະຬ / 10%Ҏ্
  8. Ͱ͸ɺܻ਺Λ੾Γସ͑ͯΈΔ 00 let formatter = NumberFormatter() formatter.numberStyle = .percent func

    myFormat(_ value: Double) -> String { formatter.maximumFractionDigits = switch value { case ..<0.01: 2 case ..<0.1: 1 default: 0 } return formatter.string(from: NSNumber(value: value)) ?? "" } print(myFormat(0.12345)) print(myFormat(0.012345)) print(myFormat(0.0012345)) 12% 1.2% 0.12% ͍͍ײ͡ ←஋ͷൣғͰ৔߹෼͚ܻͯ͠਺Λ੾Γସ͑Δ 1%ະຬ / 1%Ҏ্10%ະຬ / 10%Ҏ্
  9. ༗ޮ਺ࣈ2ܻͰ੔ܗ͢Δ 00 let formatter = NumberFormatter() formatter.numberStyle = .percent formatter.usesSignificantDigits

    = true formatter.maximumSignificantDigits = 2 func myFormat(_ value: Double) -> String { return formatter.string(from: NSNumber(value: value)) ?? "" } print(myFormat(0.12345)) print(myFormat(0.012345)) print(myFormat(0.0012345)) 12% 1.2% 0.12% ←༗ޮܻ਺2ܻʹઃఆ Αͦ͞͏…ʁ
  10. খ͍͞஋ͷͱ͖ʹ௕͘ͳͬͯ͠·͏ͱࢦఠ͞ΕΔ 00 let formatter = NumberFormatter() formatter.numberStyle = .percent formatter.usesSignificantDigits

    = true formatter.maximumSignificantDigits = 2 func myFormat(_ value: Double) -> String { return formatter.string(from: NSNumber(value: value)) ?? "" } print(myFormat(0.12345)) print(myFormat(0.012345)) print(myFormat(0.0012345)) print(myFormat(0.00012345)) print(myFormat(0.00000012345)) 12% 1.2% 0.12% 0.012% 0.000012%
  11. খ͍ܻ͞Λ࢛ࣺޒೖͯ͠ղܾ • ࠷ॳʹ΍͍ͬͯͨɺখ਺ୈ5ҐͰͷ࢛ࣺޒೖΛ࠶ͼಋೖ͠·͢ • Α͏΍͍͍͘ײ͡ʹͰ͖·ͨ͠ 00 func myFormat(_ value: Double)

    -> String { let roundedValue = round(value * 10000) / 10000 return formatter.string(from: NSNumber(value: roundedValue)) ?? "" } print(myFormat(0.12345)) print(myFormat(0.012345)) print(myFormat(0.0012345)) print(myFormat(0.00012345)) print(myFormat(0.00000012345)) ←খ਺ୈ5ҐͰ࢛ࣺޒೖ 12% 1.2% 0.12% 0.01% 0%
  12. func test_࢖༻༰ྔ͕0ͷͱ͖ʹ0ύʔηϯτදهʹͳΔ() { let usage = 0 let limit =

    1000000 * 1000 * 1000 let user = testUser(usage, limit) XCTAssertEqual(user.storageUsageFormatted, "0%") } func test_࢖༻༰ྔ͕0_01ύʔηϯτະຬͷͱ͖ʹ0ύʔηϯτදهʹͳΔ let usage = 12 * 1000 * 1000 let limit = 1000000 * 1000 * 1000 let user = testUser(usage, limit) XCTAssertEqual(user.storageUsageFormatted, "0%") } func test_࢖༻༰ྔ͕0_1ύʔηϯτະຬͷͱ͖ʹখ਺2Ґ·ͰͷදهʹͳΔ let usage = 123 * 1000 * 1000 let limit = 1000000 * 1000 * 1000 let user = testUser(usage, limit) XCTAssertEqual(user.storageUsageFormatted, "0.01%") } func test_࢖༻༰ྔ͕1ύʔηϯτະຬͷͱ͖ʹখ਺2Ґ·ͰͷදهʹͳΔ() let usage = 1234 * 1000 * 1000 let limit = 1000000 * 1000 * 1000 let user = testUser(usage, limit) XCTAssertEqual(user.storageUsageFormatted, "0.12%") } func test_࢖༻༰ྔ͕10ύʔηϯτະຬͷͱ͖ʹখ਺1Ґ·ͰͷදهʹͳΔ( ςετΛॻ͍ͯ҆৺͢Δ • Ͳ͏ͳͬͯ΄͍͔͠ΛͪΌ Μͱॻ͍ͯ֬ೝ͢Δ • ࣮ࡍͷετϨʔδ༰ྔͰද ࣔΛ֬ೝ͢Δͷ͸ඇݱ࣮త • ϋϚΔ਺஋ͳͲΛཏྻ͠ ͯɺظ଴͢ΔදࣔʹͳΔ͜ ͱΛ֬ೝ
  13. ࣗݾ঺հ glass fi ber ϐΫγϒגࣜձࣾ ৽نࣄۀ෦ iOSΤϯδχΞ • 2023೥8݄ த్ೖࣾ

    • Pastelaͱ͍͏͓ֆඳ͖ΞϓϦͷ։ൃʹैࣄ • લ৬͸ήʔϜ։ൃऀ • C++, C#Λओʹ࢖༻ • 6೥લ·Ͱ͸iOS༻ήʔϜ΋࡞͍ͬͯͨ(iOS3ʙ7ͷ͜Ζ) • ϒϥϯΫΛຒΊΔͨΊमߦத 00
  14. ුಈখ਺఺਺ͷத਎ΛݟΔ • SwiftͰ͸ .bitPatternͰුಈখ਺఺਺ͷදݱΛ ֬ೝͰ͖Δ • .exponentBitPatternͱ.signi fi candBitPatternͰࢦ਺෦ͱԾ਺෦Λݸผ ʹݟΔ͜ͱ΋Մೳ

    00 print("\(String(0.523.bitPattern, radix: 2))") print("\(String(0.523.exponentBitPattern, radix: 2))") print("\(String(0.523.significandBitPattern, radix: 2))") 11111111100000101111000110101001111110111110011101101100100011 1111111110 101111000110101001111110111110011101101100100011