-2

I'm trying to make an app that multiplies a number for a price that can be decimal or not. For example 3 * 3.50.

I'm using Swift 3. I searched for several topics here but could not add this feature to my app.

My code looks like this:

    @IBOutlet weak var valueBreja1: UITextField!
    @IBOutlet weak var quantityBreja1: UITextField!
    @IBOutlet weak var totalBreja1: UILabel!

    @IBAction func calcBreja1(_ sender: Any) {
        let a = Int(valueBreja1.text!)
        let b = Int(quantityBreja1.text!)

        let Answer = a! + b!
        totalBreja1.text = "R$\(Answer)"
    }

I wanted to show the value with decimal number after multiplication.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
ViniciusPV
  • 1,657
  • 2
  • 11
  • 7
  • 2
    There is no multiplication in your code. There is only an adition. If you would like to show a value as currency you should use `NumberFormatter` and set the numberStyle to `.currency` – Leo Dabus Jan 05 '18 at 01:38
  • `Int` stands for "integer"... which can only be whole numbers. You'll probably want to use [`Decimal`](https://developer.apple.com/documentation/foundation/decimal) for prices – Alexander Jan 05 '18 at 01:38
  • @ViniciusPV Does my answer work for you? – Xcoder Jan 06 '18 at 23:16

2 Answers2

0

Use Double instead of Int:

@IBAction func calcBreja1(_ sender: Any) {

    let a = Double(valueBreja1.text!)
    let b = Double(quantityBreja1.text!)

    let Answer = a! + b!
    totalBreja1.text = "R$\(Answer)"

}
Xcoder
  • 1,433
  • 3
  • 17
  • 37
  • Don't use `Double` for currency. You'll have a bad, bad time. – Alexander Jan 05 '18 at 01:40
  • Sorry, kind of new to Swift. May I ask why? – Xcoder Jan 05 '18 at 01:41
  • Nothing to do with Swift, it's equally applicable to any other language. E.g. in Java, `BigInteger` is usually used. The issue is that `Double` has really pesky precision issues that stem from it being based on binary floating point, as opposed to the decimal floating point humans usually expect. See https://stackoverflow.com/q/588004/3141234 – Alexander Jan 05 '18 at 01:52
  • `0.1 + 0.2 == 0.3` -> `false`. See for for yourself inthe Swift repl: `0.1 + 0.2` -> `$R0: Double = 0.30000000000000004` and `0.3` -> `$R1: Double = 0.29999999999999999` – Alexander Jan 05 '18 at 01:54
  • 1
    Humans are used to `1/3` being `0.333...`, but they're not so used to `7 / 10` being `0.69999999999999996` – Alexander Jan 05 '18 at 01:56
  • @Alexander Thanks for the input. I'll look into it more. – Xcoder Jan 05 '18 at 16:20
0

You have two problems.

First, you're creating Int values from the field strings. Int values can only hold integer values, so any fractional portion will be discarded. You need to create Double values (or some other form of floating or fractional value).

To format the output use a formatter.

//: Playground - noun: a place where people can play

import Cocoa

var str1 = "3.50"
var str2 = "3"
if let value = Double(str1) {
    if let multiplier = Int(str2) {
        let formattedResult = String(format: "%.02f", value*Double(multiplier))
    }
}
James Bucanek
  • 3,299
  • 3
  • 14
  • 30