-
(Swift) NumberFormatter 정수 / 소수 자릿수 정하기iOS💖 2024. 9. 11. 15:43
- minimumFractionDigits
: 최소 소수점 자릿수
// 1 var numberFormatter = NumberFormatter() numberFormatter.minimumFractionDigits = 0 // default numberFormatter.string(from: 123.456) // 123 numberFormatter.minimumFractionDigits = 5 numberFormatter.string(from: 123.456) // 123.45600 // 2 var priceString = 123456789 func convertToCurrencyFormat(price: Int) -> String { numberFormatter.numberStyle = .decimal // 십진수 numberFormatter.minimumFractionDigits = 0 return numberFormatter.string(from: NSNumber(value: price))! } converToCurrencyFormat(priceString) // 123,456,789- maximumFractionDigits
: 최대 소수점 자릿수
var NumberFormatter = NumberFormatter() numberFormatter.maximumFractionDigits = 0 // default numberFormatter.string(from: 123.456) // 123 numberFormatter.maximumFractionDigits = 3 numberFormatter.string(from: 123.456789) // 123.456- minimumIntegerDigits
: 최소 정수 자릿수
var numberFormatter = NumberFormatter() numberFormatter.minimumIntegerDigits = 0 // default numberFormatter.string(from: 123) // 123 numberFormatter.minimumIntegerDigits = 5 numberFormatter.string(from: 123) // 00123- maximumIntegerDigits
: 최대 정수 자릿수
var numberFormatter = NumberFormatter numberFormatter.maximumIntegerDigits = 42 // default numberFormatter.string(from: 12345) // 12345 numberFormatter.maximumIntegerDigits = 3 numberFormatter.string(from: 12345) // 345
NumberFormatter
https://developer.apple.com/documentation/foundation/numberformatter
NumberFormatter | Apple Developer Documentation
A formatter that converts between numeric values and their textual representations.
developer.apple.com
minimumFractionDigits
https://developer.apple.com/documentation/foundation/numberformatter/1410459-minimumfractiondigits
minimumFractionDigits | Apple Developer Documentation
The minimum number of digits after the decimal separator.
developer.apple.com
'iOS💖' 카테고리의 다른 글
(Swift) sorted() (2) 2024.09.11 (Swift) DateFormatter (1) 2024.09.11 (Swift) CollectionView Estimate Size 코드 설정 (0) 2024.09.10 (Swift) CollectionView contentInset (padding 주는 방법) (4) 2024.09.10 (iOS) Life Cycle (AppDelegate / SceneDelegate) (4) 2024.09.06 - minimumFractionDigits