Help with decimal

Hi I’m trying to solve the following errors I wrote this code but to give the right result, I need to declare its values as Decimal to get the result, I did this test in the playground but it worked fine as in the image below but I can not pass it for my original project below is the image of the tests, the app and the code if anyone can help me thank you very much

// ViewController.swift

// Calculadora de Bitcoin

//

// Created by Tiago coelho de souza on 01/01/19.

// Copyright © 2019 tiago. All rights reserved.

//

import UIKit

class ViewController: UIViewController,UITextFieldDelegate {

@IBOutlet weak var btcResultado: UILabel!

@IBOutlet weak var valorTotal: UITextField!

@IBOutlet weak var valorInvestido: UITextField!



@IBAction func cacularSubtracao(_ sender: Any) {

    

    if let valorT = valorTotal.text{

        if let valorI = valorInvestido.text{

            

            

            //validar numeros Digitados

            let validaCampo = self.validarCampos(valorT: valorT, valorI: valorI)

            

            if validaCampo {

                

                if let valor1 = Float(valorT){

                    if let valor2 = Float(valorI){

                        

                        let Resultado = Double(valor1 - valor2)

                        btcResultado.text = String(Resultado)

                    }

                }

                

            }else{

                

                btcResultado.text = "Por Favor prencha os Campos"

            }

}

        

      

}

}

func validarCampos(valorT:String,valorI:String) -> Bool{

    

    var camposValidados = true

    if valorT.isEmpty{

        camposValidados = false

        

    }else if valorI.isEmpty{

        camposValidados = false

        

    }

    return camposValidados

}

override func viewDidLoad() {

    super.viewDidLoad()

    // Do any additional setup after loading the view, typically from a nib.



    self.valorTotal.delegate = self

    self.valorInvestido.delegate = self

}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    self.view.endEditing(true)

}



func textFieldShouldReturn(_ textField: UITextField) -> Bool {

    textField.resignFirstResponder()

    return(true)

   }

}

https://udemy-images.s3.amazonaws.com/redactor/raw/2019-01-02_13-28-11-c039f0f32ccf7cb581615526f6f5053a.jpg

2019-01-09_21-01-44-1b11a6d00dc8bac5aff4399b291e9377

Hi @tiagowt,
have you tried casting it as Decimal(valor1)?

so you can have

   let resultado = Decimal(valor1) - Decimal(valor2)

if there is something else that you are after, then it is not very clear in your question.

cheers,

Hi, I tried and it worked fine but this line of error after I entered the codes:

btcResultado.text = String(Resultado)

Hi @tiagowt, what is the error that you are seeing for that line?

it is not possible to convert string to decimal because the complete code here is like this

let Resultado = Decimal(valor1) - Decimal(valor2)
btcResultado.text = String (Resultado)

Hi @tiagowt,
yes it will give you a compiler error as swift does not know how to convert a Decimal to a String. You will have to convert it in a different way.

The easiest way is to embed it into a string like so, I have done this in a terminal swift REPL because Swift playgrounds on the iPad does not show the result or print statements and xcode was taking for ever trying to start the simulator.

  1> import Foundation
  2> let num = Decimal(5.23) 
num: Decimal = {
  _mantissa = {
    0 = 523
    1 = 0
    2 = 0
    3 = 0
    4 = 0
    5 = 0
    6 = 0
    7 = 0
  }
}
  3> let str = "Value is \(num)"
str: String = "Value is 5.23"

cheers,

@tiagowt,
I have one question in mind, why is it that using a Decimal is so important for you that a Float or a Double cannot be used instead? If it is to eliminate the floating point error, that will not get resolved irrespective of the type of data type you will choose because the way numbers are stored will cause the errors. Some languages use specific fixes for the same, Swift unfortunately does not. I had written to the Swift evolution to get that fixed only to get a response that “it is how it it”.

cheers,

@tiagowt,
Also if you look at the Apple Documentation page for Decimal, there is a function called NSDecimalString that allows you to convert a Decimal (NSDecimal for Objective-C) into a string.

Details here Apple Developer Documentation

cheers,

This topic was automatically closed after 166 days. New replies are no longer allowed.