Swift: Trunc a Floating Number to Show It in a Label

Round up double to 2 decimal places

Use a format string to round up to two decimal places and convert the double to a String:

let currentRatio = Double (rxCurrentTextField.text!)! / Double (txCurrentTextField.text!)!
railRatioLabelField.text! = String(format: "%.2f", currentRatio)

Example:

let myDouble = 3.141
let doubleStr = String(format: "%.2f", myDouble) // "3.14"

If you want to round up your last decimal place, you could do something like this (thanks Phoen1xUK):

let myDouble = 3.141
let doubleStr = String(format: "%.2f", ceil(myDouble*100)/100) // "3.15"

Rounding a double value to x number of decimal places in swift

You can use Swift's round function to accomplish this.

To round a Double with 3 digits precision, first multiply it by 1000, round it and divide the rounded result by 1000:

let x = 1.23556789
let y = Double(round(1000 * x) / 1000)
print(y) /// 1.236

Unlike any kind of printf(...) or String(format: ...) solutions, the result of this operation is still of type Double.

EDIT:

Regarding the comments that it sometimes does not work, please read this: What Every Programmer Should Know About Floating-Point Arithmetic

Swift - How to remove a decimal from a float if the decimal is equal to 0?

Swift 3/4:

var distanceFloat1: Float = 5.0
var distanceFloat2: Float = 5.540
var distanceFloat3: Float = 5.03

extension Float {
var clean: String {
return self.truncatingRemainder(dividingBy: 1) == 0 ? String(format: "%.0f", self) : String(self)
}
}

print("Value \(distanceFloat1.clean)") // 5
print("Value \(distanceFloat2.clean)") // 5.54
print("Value \(distanceFloat3.clean)") // 5.03

Swift 2 (Original answer)

let distanceFloat: Float = (currentUser.distance! as NSString).floatValue
distanceLabel.text = String(format: distanceFloat == floor(distanceFloat) ? “%.0f" : "%.1f", distanceFloat) + "Km"

Or as an extension:

extension Float {
var clean: String {
return self % 1 == 0 ? String(format: "%.0f", self) : String(self)
}
}

Make a float only show two decimal places

It is not a matter of how the number is stored, it is a matter of how you are displaying it. When converting it to a string you must round to the desired precision, which in your case is two decimal places.

E.g.:

NSString* formattedNumber = [NSString stringWithFormat:@"%.02f", myFloat];

%.02f tells the formatter that you will be formatting a float (%f) and, that should be rounded to two places, and should be padded with 0s.

E.g.:

%f = 25.000000
%.f = 25
%.02f = 25.00

Precision String Format Specifier In Swift

My best solution so far, following from David's response:

import Foundation

extension Int {
func format(f: String) -> String {
return String(format: "%\(f)d", self)
}
}

extension Double {
func format(f: String) -> String {
return String(format: "%\(f)f", self)
}
}

let someInt = 4, someIntFormat = "03"
println("The integer number \(someInt) formatted with \"\(someIntFormat)\" looks like \(someInt.format(someIntFormat))")
// The integer number 4 formatted with "03" looks like 004

let someDouble = 3.14159265359, someDoubleFormat = ".3"
println("The floating point number \(someDouble) formatted with \"\(someDoubleFormat)\" looks like \(someDouble.format(someDoubleFormat))")
// The floating point number 3.14159265359 formatted with ".3" looks like 3.142

I think this is the most Swift-like solution, tying the formatting operations directly to the data type. It may well be that there is a built-in library of formatting operations somewhere, or maybe it will be released soon. Keep in mind that the language is still in beta.

Format float value with 2 decimal places

You can use standard string formatting specifiers to round to an arbitrary number of decimal places. Specifically %.nf where n is the number of decimal places you require:

let twoDecimalPlaces = String(format: "%.2f", 10.426123)

Assuming you want to display the number on each of the l* labels:

@IBAction func Berechnen(sender: AnyObject) {

var Zahl = (txt.text as NSString).floatValue

l5.text = String(format: "%.2f", (Zahl / 95) * 100)
l10.text = String(format: "%.2f", (Zahl / 90) * 100)
l15.text = String(format: "%.2f", (Zahl / 85) * 100)
l20.text = String(format: "%.2f", (Zahl / 80) * 100)
l25.text = String(format: "%.2f", (Zahl / 75) * 100)
l30.text = String(format: "%.2f", (Zahl / 70) * 100)
l35.text = String(format: "%.2f", (Zahl / 65) * 100)
l40.text = String(format: "%.2f", (Zahl / 60) * 100)
}

How to check if UILabel is truncated?

You can calculate the width of the string and see if the width is greater than label.bounds.size.width

NSString UIKit Additions has several methods for computing the size of the string with a specific font. However, if you have a minimumFontSize for your label that allows the system to shrink the text down to that size. You may want to use sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: in that case.

CGSize size = [label.text sizeWithAttributes:@{NSFontAttributeName:label.font}];
if (size.width > label.bounds.size.width) {
...
}

Check for Truncation in UILabel - iOS, Swift

You can use the sizeWithAttributes method from NSString to get the number of lines your UILabel has. You will have to cast your label text to NSString first to use this method:

func countLabelLines(label:UILabel)->Int{

if let text = label.text{
// cast text to NSString so we can use sizeWithAttributes
var myText = text as NSString
//A Paragraph that we use to set the lineBreakMode.
var paragraph = NSMutableParagraphStyle()
//Set the lineBreakMode to wordWrapping
paragraph.lineBreakMode = NSLineBreakMode.ByWordWrapping

//Calculate the size of your UILabel by using the systemfont and the paragraph we created before. Edit the font and replace it with yours if you use another
var labelSize = myText.sizeWithAttributes([NSFontAttributeName : UIFont.systemFontOfSize(17), NSParagraphStyleAttributeName : paragraph.copy()])

//Now we return the amount of lines using the ceil method
var lines = ceil(CGFloat(size.height) / label.font.lineHeight)
return Int(lines)
}

return 0

}

Edit

If this method doesn't work for you because your label hasn't a fixed width, you can use boundingRectWithSize to get the size of the label and use that with the ceil method.

func countLabelLines(label:UILabel)->Int{

if let text = label.text{
// cast text to NSString so we can use sizeWithAttributes
var myText = text as NSString

//Set attributes
var attributes = [NSFontAttributeName : UIFont.systemFontOfSize(UIFont.systemFontSize())]

//Calculate the size of your UILabel by using the systemfont and the paragraph we created before. Edit the font and replace it with yours if you use another
var labelSize = myText.boundingRectWithSize(CGSizeMake(label.bounds.width, CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: attributes, context: nil)

//Now we return the amount of lines using the ceil method
var lines = ceil(CGFloat(labelSize.height) / label.font.lineHeight)
return Int(lines)
}

return 0

}

How to change truncate characters in UILabel?

I have written a custom truncating class that you can pop into you code where ever. Just use this method below. it will return true if truncation has taken place, and MaxWidth can be left as 0 if you just want to use the labels default frame width. Put maxWidth as something less than the frames width to shorten it within its frames bounds.

Swift 2 (with some swift 3 comments for converting)

usage:

Truncater.replaceElipsis(forLabel: label, withString: "???")
let didTruncate = Truncater.replaceElipsis(forLabel: label, withString: "1234", andMaximumWidth: 50) //maxWidth is not number of chars, but label width in CGFloat

class:

import UIKit

class Truncater {

class func replaceElipsis(forLabel label:UILabel, withString replacement:String) -> Bool {
return replaceElipsis(forLabel: label, withString: replacement, andMaximumWidth:0)
}

class func replaceElipsis(forLabel label:UILabel, withString replacement:String, andMaximumWidth width:CGFloat) -> Bool {

if(label.text == nil){
return false
}

let origSize = label.frame;
var useWidth = width

if(width <= 0){
useWidth = origSize.width //use label width by default if width <= 0
}

label.sizeToFit()
let labelSize = label.text!.sizeWithAttributes([NSFontAttributeName: label.font]) //.size(attributes: [NSFontAttributeName: label.font]) for swift 3

if(labelSize.width > useWidth){

let original = label.text!;
let truncateWidth = useWidth;
let font = label.font;
let subLength = label.text!.characters.count

var temp = label.text!.substringToIndex(label.text!.endIndex.advancedBy(-1)) //label.text!.substring(to: label.text!.index(label.text!.endIndex, offsetBy: -1)) for swift 3
temp = temp.substringToIndex(temp.startIndex.advancedBy(getTruncatedStringPoint(subLength, original:original, truncatedWidth:truncateWidth, font:font, length:subLength)))
temp = String.localizedStringWithFormat("%@%@", temp, replacement)

var count = 0

while temp.sizeWithAttributes([NSFontAttributeName: label.font]).width > useWidth {

count+=1

temp = label.text!.substringToIndex(label.text!.endIndex.advancedBy(-(1+count)))
temp = temp.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) //remove this if you want to keep whitespace on the end
temp = String.localizedStringWithFormat("%@%@", temp, replacement)
}

label.text = temp;
label.frame = origSize;
return true;
}
else {

label.frame = origSize;
return false
}
}

class func getTruncatedStringPoint(splitPoint:Int, original:String, truncatedWidth:CGFloat, font:UIFont, length:Int) -> Int {

let splitLeft = original.substringToIndex(original.startIndex.advancedBy(splitPoint))

let subLength = length/2

if(subLength <= 0){
return splitPoint
}

let width = splitLeft.sizeWithAttributes([NSFontAttributeName: font]).width

if(width > truncatedWidth) {
return getTruncatedStringPoint(splitPoint - subLength, original: original, truncatedWidth: truncatedWidth, font: font, length: subLength)
}
else if (width < truncatedWidth) {
return getTruncatedStringPoint(splitPoint + subLength, original: original, truncatedWidth: truncatedWidth, font: font, length: subLength)
}
else {
return splitPoint
}
}
}

Objective C

+ (bool) replaceElipsesForLabel:(UILabel*) label With:(NSString*) replacement MaxWidth:(float) width 

class:

//=============================================Header=====================================================
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface CustomTruncater : NSObject

+ (bool) replaceElipsesForLabel:(UILabel*) label With:(NSString*) replacement MaxWidth:(float) width;

@end

//========================================================================================================

#import "CustomTruncater.h"

@implementation CustomTruncater

static NSString *original;
static float truncateWidth;
static UIFont *font;
static int subLength;

+ (bool) replaceElipsesForLabel:(UILabel*) label With:(NSString*) replacement MaxWidth:(float) width {

CGRect origSize = label.frame;

float useWidth = width;

if(width <= 0)
useWidth = origSize.size.width; //use label width by default if width <= 0

[label sizeToFit];
CGSize labelSize = [label.text sizeWithFont:label.font];

if(labelSize.width > useWidth) {

original = label.text;
truncateWidth = useWidth;
font = label.font;
subLength = label.text.length;

NSString *temp = [label.text substringToIndex:label.text.length-1];
temp = [temp substringToIndex:[self getTruncatedStringPoint:subLength]];
temp = [NSString stringWithFormat:@"%@%@", temp, replacement];

int count = 0;

while([temp sizeWithFont:label.font].width > useWidth){

count++;

temp = [label.text substringToIndex:(label.text.length-(1+count))];
temp = [temp stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; //remove this if you want to keep whitespace on the end
temp = [NSString stringWithFormat:@"%@%@", temp, replacement];
}

label.text = temp;
label.frame = origSize;
return true;
}
else {
label.frame = origSize;
return false;
}
}

+ (int) getTruncatedStringPoint:(int) splitPoint {

NSString *splitLeft = [original substringToIndex:splitPoint];
subLength /= 2;

if(subLength <= 0)
return splitPoint;

if([splitLeft sizeWithFont:font].width > truncateWidth){
return [self getTruncatedStringPoint:(splitPoint - subLength)];
}
else if ([splitLeft sizeWithFont:font].width < truncateWidth) {
return [self getTruncatedStringPoint:(splitPoint + subLength)];
}
else {
return splitPoint;
}
}

@end


Related Topics



Leave a reply



Submit