Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format float value with 2 decimal places [duplicate]

Tags:

swift

xcode6

How can I round the result to 2 decimal places and show it on the result label? I found some statements, but I´m new to Swift and it is actually difficult for me to rebuild the samples for my project.

import UIKit  class ViewController: UIViewController {          @IBOutlet var txt: UITextField!          @IBOutlet var l5: UILabel!     @IBOutlet var l10: UILabel!     @IBOutlet var l15: UILabel!     @IBOutlet var l20: UILabel!     @IBOutlet var l25: UILabel!     @IBOutlet var l30: UILabel!     @IBOutlet var l35: UILabel!     @IBOutlet var l40: UILabel!          @IBAction func Berechnen(sender: AnyObject) {                  var Zahl = (txt.text as NSString).floatValue                  l5.text  = "\((Zahl / 95) * (100))"         l10.text = "\((Zahl / 90) * (100))"         l15.text = "\((Zahl / 85) * (100))"         l20.text = "\((Zahl / 80) * (100))"         l25.text = "\((Zahl / 75) * (100))"         l30.text = "\((Zahl / 70) * (100))"         l35.text = "\((Zahl / 65) * (100))"         l40.text = "\((Zahl / 60) * (100))"     }          func textFieldShouldReturn(textField: UITextField) -> Bool {         self.view.endEditing(true)         return false     }  } 
like image 231
Andreas Avatar asked Aug 07 '15 10:08

Andreas


People also ask

How do you keep a float up to 2 decimal places?

format("%. 2f", 1.23456); This will format the floating point number 1.23456 up-to 2 decimal places, because we have used two after decimal point in formatting instruction %.

How do you make a double show with two decimal places?

format(“%. 2f”) We also can use String formater %2f to round the double to 2 decimal places.

What is the number 2.738 correct to 2 decimal places?

What is 2.738 Round to Two Decimal Places? In the given number 2.738, the digit at the thousandths place is 8, so we will add 1 to the hundredths place digit. So, 3+1=4. Therefore, the value of 2.738 round to two decimal places is 2.74.

How do I limit a float to two decimal places in Excel?

By using a button: Select the cells that you want to format. On the Home tab, click Increase Decimal or Decrease Decimal to show more or fewer digits after the decimal point.


Video Answer


1 Answers

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) } 
like image 70
Steve Wilford Avatar answered Sep 27 '22 20:09

Steve Wilford