glassfiber
August 23, 2024
250

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

August 23, 2024

## Transcript

1. ### glass fi ber (ϐΫγϒגࣜձࣾʣ ਺஋Λจࣈྻʹ੔ܗ͢Δࡍͷ མͱ݀͠ͱͦͷղܾࡦ iOSDC Japan 2024 2024/08/23

Track A ϧʔΩʔζLT

4. ### ࢖༻཰Λύʔηϯτදࣔ͢Δ • খ਺ୈ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ҐͰ࢛ࣺޒೖ͢Δ
5. ### • ஋ʹΑͬͯ͸ظ଴͢ΔදࣔʹͳΒͳ͍͜ͱ͕൑໌ • ͨͱ͑͹஋͕0.523ͷͱ͖ • খ਺఺ҎԼͷܻ਺͕ͱͯ΋௕͘ͳͬͯ͠·͏ 52.300000000000004% ʮͳΜ͔ද͕͓͔ࣔ͘͠ͳΔʯͱ͍͏ใࠂ͕ 00 let

usageRate = 0.523 let value = round(usageRate * 10000) / 10000 let percentValue = value * 100 print("\(percentValue)%")

00
7. ### ܻ਺Λࢦఆͨ͠ΒΑ͍ͷͰ͸ • String(format:)Ͱখ਺఺ҎԼͷܻ਺Λࢦఆͨ͠ΒͲ͏Ͱ͠ΐ͏ • ݁Ռ͸ɺܻ਺͕ݻఆͳͷͰɺ຤ඌʹ0͕ͭ͘ • ͜ΕͰ΋͍͍͕ɺ΋͏ͪΐͬͱ͍͍ײ͡ʹ͍ͨ͠ • ຤ඌͷ0͸ͳ͍ͨ͘͠ 00

let usageRate = 0.523 let value = round(usageRate * 10000) / 10000 print("\(String(format: "%.2f", value * 100))%") 52.30% ↑খ਺఺ҎԼ2ܻ
8. ### ਺஋ͷ੔ܗͱ͍͑͹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ܻ
9. ### খ਺఺ҎԼ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%
10. ### Ͱ͸ɺܻ਺Λ੾Γସ͑ͯΈΔ 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%Ҏ্
11. ### Ͱ͸ɺܻ਺Λ੾Γସ͑ͯΈΔ 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% มΘͬͯͳ͍…ʁ
12. ### Ͱ͸ɺܻ਺Λ੾Γସ͑ͯΈΔ 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% มΘͬͯͳ͍…ʁ ←ˋͷ਺஋Ͱൺֱ͢Δؒҧ͍ (Α͘΍Δ)
13. ### Ͱ͸ɺܻ਺Λ੾Γସ͑ͯΈΔ 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%Ҏ্
14. ### Ͱ͸ɺܻ਺Λ੾Γସ͑ͯΈΔ 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%Ҏ্
15. ### දࣔ͸͍͍ײ͚ͩ͡Ͳɺίʔυ͕ؾʹೖΒͳ͍ • switchͰ෼͚Δͷ͸ͳΜ͔ΠϚΠν 00 formatter.maximumFractionDigits = switch value { case

..<0.01: 2 case ..<0.1: 1 default: 0 }
16. ### Ͳ͏͍ͨ͠ͷ͔ߟ͑ͯΈΔ • ༗ޮ਺ࣈ͕2ܻɺͱ͍͏͜ͱͰΑͦ͞͏…ʁ • NumberFomatterʹ͸༗ޮܻ਺Λࢦఆ͢Δػೳ͕͋Δ (maximumSigni fi cantDigits) 00 →

→ → 12% 1.2% 0.12% 0.12345 0.012345 0.0012345 ͜ͷ஋ͷͱ͖ ͜͏͍ͨ͠
17. ### ༗ޮ਺ࣈ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ܻʹઃఆ Αͦ͞͏…ʁ
18. ### খ͍͞஋ͷͱ͖ʹ௕͘ͳͬͯ͠·͏ͱࢦఠ͞ΕΔ 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%
19. ### খ͍͞஋ͷͱ͖ʹ௕͘ͳͬͯ͠·͏ • খ͍͞஋ͷͱ͖ʹ΋͔ͬ͠Γ༗ޮ਺ࣈ2ܻͰग़ΔͷͰ௕͘ͳΔ • খ਺෦ͷܻ਺ࢦఆ(maximumFractionDigits) ͸จࣈྻͷ௕੍͕͞ݶ͞ ΕΔ͕ɺ༗ޮܻ਺ࢦఆ(maximumSigni fi cantDigits) ͸ͦ͏Ͱ͸ͳ͍

• ͦΕ͸ͦ͏ 00 print(myFormat(0.00012345)) print(myFormat(0.00000012345)) 0.012% 0.000012%
20. ### খ͍ܻ͞Λ࢛ࣺޒೖͯ͠ղܾ • ࠷ॳʹ΍͍ͬͯͨɺখ਺ୈ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%
21. ### 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Ґ·ͰͷදهʹͳΔ( ςετΛॻ͍ͯ҆৺͢Δ • Ͳ͏ͳͬͯ΄͍͔͠ΛͪΌ Μͱॻ͍ͯ֬ೝ͢Δ • ࣮ࡍͷετϨʔδ༰ྔͰද ࣔΛ֬ೝ͢Δͷ͸ඇݱ࣮త • ϋϚΔ਺஋ͳͲΛཏྻ͠ ͯɺظ଴͢ΔදࣔʹͳΔ͜ ͱΛ֬ೝ
22. ### ·ͱΊ • ුಈখ਺఺਺ͷѻ͍͸೉͍͠ • ਺஋Λύʔηϯτදهʹ੔ܗ͢Δࡍʹ100ഒͯ͠Ͳ͏ʹ͔͠Α͏ͱ͠ ͯ͸͍͚ͳ͍ • NumberFormatterͳͲΛ࢖͍ͬͯͩ͘͞ • ༗ޮܻ਺ͷࢦఆ͸ศར͕ͩɺจࣈྻͷ௕͞Λ੍ݶͯ͘͠Εͳ͍ͷͰ஫ҙ

͕ඞཁ • maximumFractionDigits ͱಉ͡Α͏ͳ΋ͷͱࢥͬͯ͠·͏ϫφ • ςετΛॻ͍ͯ҆৺͠Α͏ 00
23. ### ࣗݾ঺հ glass fi ber ϐΫγϒגࣜձࣾ ৽نࣄۀ෦ iOSΤϯδχΞ • 2023೥8݄ த్ೖࣾ

• Pastelaͱ͍͏͓ֆඳ͖ΞϓϦͷ։ൃʹैࣄ • લ৬͸ήʔϜ։ൃऀ • C++, C#Λओʹ࢖༻ • 6೥લ·Ͱ͸iOS༻ήʔϜ΋࡞͍ͬͯͨ(iOS3ʙ7ͷ͜Ζ) • ϒϥϯΫΛຒΊΔͨΊमߦத 00

25. ### ුಈখ਺఺਺ͷத਎ΛݟΔ • ුಈখ਺఺਺: IEEE754 (Double : ഒਫ਼౓ɺFloat: ୯ਫ਼౓) • ුಈখ਺఺਺ͷܭࢉΛϏοτ୯ҐͰݟΕΔπʔϧ

• IEEE 754 Calculator (http://weitz.de/ieee/) 00