.decimalPlaces()
๐พ ็จๅผ๏ผ replit
// 2022.02.15
import Foundation // for NSString: String(format:)
extension FloatingPoint where Self: CVarArg {
/// show a floating-point number to specified decimal places.
/// ```
/// (1.2345).decimalPlaces(3) // "1.234"
/// (1.2).decimalPlaces(3) // "1.200"
/// ```
public func decimalPlaces(_ n: Int) -> String {
return String(format: "%.\(n)f", self)
}
}
๐็ฏไพ๏ผ
(1.2345).decimalPlaces(3) // "1.234"
(1.2).decimalPlaces(3) // "1.200"
Float(45.678).decimalPlaces(2) // "45.68"
is a FloatingPoint extension.
uses a String initializer from Foundation.
needs specifier / formatter.
Foundation โฉ
String โฉ String(format:_:) - ๐ No overview available. (2022.02.11)
Last updated