UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

math calculation on string - swift

Forums > Swift

Hello! How we can compute strings in swift. For example, "5*4-1+3"

3      

Like this:

let expression = NSExpression(format:"5*4-1+3")
let value = expression.expressionValue(with: nil, context: nil) as? Int

value returned will be an Optional Int

See much more that can be done with NSExpression here: NSExpression - NSHipster (though be warned that it's several years old and some of the functions have since been renamed)

3      

Thank you, but I got 6 as a result for this expression "2x3-1/5" . it shuld be 1. for example: "2x3" = 6 ; "6-1" = 5 ; "5/5" = 1

3      

I got 6 as a result for this expression "2x3-1/5" . it shuld be 1. for example: "2x3" = 6 ; "6-1" = 5 ; "5/5" = 1

I think it's because 1/5 is being interpreted as a fraction. 2*3 = 6 then it is subtracting 1/5. But since the answer is an Int, it's coming out as 6.

If you use parentheses to properly indicate precedence, it will give you the correct answer:

"(2*3-1)/5"

3      

The answer to "2 * 3 - 1 / 5" equal 5.8 there as Int would display 6 because times multiple and division are done first before addition and subtraction.

2 multiple 3 equal 6

1 divided 5 equal 0.2

6 minus 0.2 equal 5.8

as typecast to Int would round to nearest whole number as 6

if you want answer one do "(2 * 3 - 1) / 5" equal 1

3      

when I applied let expression = NSExpression(format:formula) I got "(2 * 3) - (1 / 5)", this variable (formula) is not fixed as "2 * 3 - 1 / 5" it is jus example for many expression styles

thnk you

3      

NSExpression - NSHipster describes several other ways to use NSExpression. I am not understanding how to use sttdev or more interestingly modulus calculations. Any assistance?

My feeble attempt:

 let expn = NSExpression(forFunction: "modulus:",
                        arguments:[NSExpression(modulus: "10", by:"5")])
print (expn)
let valueForOperand = expn.expressionValue(with: nil, context: nil) as? Int
print (valueForOperand!)

3      

Change:

let expn = NSExpression(forFunction: "modulus:",
                        arguments:[NSExpression(modulus: "10", by:"5")])

to:

let expn = NSExpression(forFunction: "modulus:by:",
                        arguments:[
                            NSExpression(forConstantValue: 10),
                            NSExpression(forConstantValue: 5)
                        ])

You can see what arguments each funnction takes here.

4      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.