Swift Kotlin 数値 カンマ区切り

2023/03/17に公開

ChatGPTの回答を、Tipsとして保存

swift
import UIKit

let formatter = NumberFormatter()
formatter.numberStyle = .decimal
let number = 1234567
let formattedNumber = formatter.string(from: NSNumber(value: number))
print(formattedNumber) // "1,234,567"
kotlin
import java.text.NumberFormat
import java.util.*

fun main() {
    val number = 1234567.89
    val nf = NumberFormat.getNumberInstance(Locale.getDefault())
    val formattedNumber = nf.format(number)
    println(formattedNumber)
}

Discussion