Leading Zeros for Float in Swift

Leading zeros for float in Swift

If you want a quick and dirty solution:

String(format: "%05.2f", floatChar)

From this documentation: 0 means pad it with leading zero. 5 is the minimum width, including the dot character.


The longer answer is that some locales use comma as the decimal separator. If you want to match the user's locale, use NumberFormatter:

let formatter = NumberFormatter()
formatter.minimumFractionDigits = 2
formatter.maximumFractionDigits = 2
formatter.minimumIntegerDigits = 2

formatter.string(from: NSNumber(value: floatChar))!

Add leading and trailing zeroes to a string/label in swift

You simply combine the two:

String(format: "%05.2f", someDouble)

The 0 means fill with leading zeros as needed.

The 5 means you want the final output to be at least 5 characters, include the decimal point.

The .2 means you want two decimal places.

If this is a number you are showing to a user then you should probably use NumberFormatter so the decimal is properly formatted for the user's locale.

Leading zeros for Int in Swift

Assuming you want a field length of 2 with leading zeros you'd do this:

import Foundation

for myInt in 1 ... 3 {
print(String(format: "%02d", myInt))
}

output:

01
02
03

This requires import Foundation so technically it is not a part of the Swift language but a capability provided by the Foundation framework. Note that both import UIKit and import Cocoa include Foundation so it isn't necessary to import it again if you've already imported Cocoa or UIKit.


The format string can specify the format of multiple items. For instance, if you are trying to format 3 hours, 15 minutes and 7 seconds into 03:15:07 you could do it like this:

let hours = 3
let minutes = 15
let seconds = 7
print(String(format: "%02d:%02d:%02d", hours, minutes, seconds))

output:

03:15:07

Format string with trailing zeros removed for x decimal places in Swift

You have to use NumberFormatter:

    let formatter = NumberFormatter()
formatter.minimumFractionDigits = 0
formatter.maximumFractionDigits = 2
print(formatter.string(from: 1.0000)!) // 1
print(formatter.string(from: 1.2345)!) // 1.23

This example will print 1 for the first case and 1.23 for the second; the number of minimum and maximum decimal places can be adjusted as you need.

I Hope that helps you!

SWIFT: using / with some numbers results zero

Use a format string with your number to manipulate how many decimal places to show:

var x:Float = 66 / 130
var y:Double = 66 / 130
print(String(format: "%.2f, %.2f", x, y))

Result:

0.51, 0.51

Other useful variations:

var x = 66 / 130 // 0.5076923076923077
var y = Double(round(1000 * x) / 1000) // 3 digits precision
print(y) // 0.508
print(floor(100 * y) / 100) // 0.5
print(ceil(100 * y) / 100) // 0.51
print(String(format: "%.2f", y)) // 0.51
print(String(format: "%.2f", floor(100 * y) / 100)) // 0.50
print(String(format: "%.2f", ceil(100 * y) / 100)) // 0.51

Using 3 digits precision then reducing to two digits allows you to control the floor and ceiling while at the same time having the desired amount of digits displayed after the decimal point.

Dropping the leading zero before the decimal point:

var t = String(format: "%.2f", ceil(100 * y) / 100)
print(String(t.characters.dropFirst())) // .51

Swift - Remove Trailing Zeros From Double

In Swift 4 you can do it like that:

extension Double {
func removeZerosFromEnd() -> String {
let formatter = NumberFormatter()
let number = NSNumber(value: self)
formatter.minimumFractionDigits = 0
formatter.maximumFractionDigits = 16 //maximum digits in Double after dot (maximum precision)
return String(formatter.string(from: number) ?? "")
}
}

example of use: print (Double("128834.567891000").removeZerosFromEnd())
result: 128834.567891

You can also count how many decimal digits has your string:

import Foundation

extension Double {
func removeZerosFromEnd() -> String {
let formatter = NumberFormatter()
let number = NSNumber(value: self)
formatter.minimumFractionDigits = 0
formatter.maximumFractionDigits = (self.components(separatedBy: ".").last)!.count
return String(formatter.string(from: number) ?? "")
}
}


Related Topics



Leave a reply



Submit