Append to Double

Hello
Is there a way to append a number to a Double, like this:
Code Block
var num: Double = 1
var num2 = num.append(1)
//num2 should be 11

Thank you
Answered by OOPer in 668547022
As already suggested, you should better hold the String representation of displayed digits and the Double value of it separately, but mutually dependent.

In your case, computed property will work:
Code Block
struct ContentView: View {
//...
private var gridItemLayout = [GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())]
//@State var res: Double = 0
@State var digits: String = "0"
var res: Double {
get {
digits.hasSuffix(".")
? Double(digits.dropLast()) ?? 0.0
: Double(digits) ?? 0.0
}
nonmutating set {
digits = String(format: "%.10g", newValue) //<- May need better formatting
}
}
@State var previousVar: Double = 0
//...
var body: some View {
VStack{
Spacer()
HStack{
Spacer()
Text("\(digits)") //<-
.font(.system(size: 50))
.bold()
.padding()
}
//...
switch numeri[i][j] {
case Nums.AC:
operat = Nums.AC;
res = 0
case Nums.uguale:
operat = Nums.uguale;
res = final
case Nums.somma:
operat = Nums.somma;
previousVar = res;
res = 0
con = 0
case Nums.meno:
operat = Nums.meno;
previousVar = res;
res = 0
con = 0
case Nums.divisione:
operat = Nums.divisione;
previousVar = res;
res = 0
con = 0
case Nums.moltiplicazione:
operat = Nums.moltiplicazione;
previousVar = res;
res = 0
con = 0
case Nums.percentuale:
operat = Nums.percentuale;
res = res / 100
case Nums.piùMeno:
operat = Nums.piùMeno;
//res = res * -1;
if digits.hasPrefix("-") {
digits = String(digits.dropFirst())
} else {
digits = "-" + digits
}
con = 0
case Nums.virgola:
operat = Nums.virgola;
//res = Double(String(res) + ".") ?? 0
if !digits.contains(".") {
digits += "."
}
default:
//res = Double(numeri[i][j].rawValue) ?? 0
if digits == "0" {
digits = numeri[i][j].rawValue
} else {
digits += numeri[i][j].rawValue
}
con += 1
}
//...
}.padding(2)
}
}


You may need to improve many parts, but with this, you can append that number to res.
Yes it is, but not this way.
You need to convert first to String, then append then convert back to Double.

Such as:
Code Block
var num: Double = 1
let numString = String(num)
let numStrPlus = "1" + numString
var num2 = Double(numStrPlus)! // num.append(1)
print(num2)

You can have a more condensed form:
Code Block
var num: Double = 1
var num2 = Double("1" + String(num)) ?? 0

or
Code Block
var num2 = Double(String(1) + String(num)) ?? 0

If you really want to write it as append, you can define an extension to Double:
Code Block
extension Double {
func append(_ val: Int) -> Double {
return Double(String(val) + String(self)) ?? 0
}
}
num2 = num.append(1)

Sorry, I didn't make myself clear
I made a calculator and when I tap the numbers, they don't append to each other, how do I solve it
This is what I am doing:
Code Block
//
// ContentView.swift
// Calcolatrice
//
// Created by Jad Taljabini on 26/03/21.
//
import SwiftUI
import Combine
enum Nums: String{
case uno = "1"
case due = "2"
case tre = "3"
case quattro = "4"
case cinque = "5"
case sei = "6"
case sette = "7"
case otto = "8"
case nove = "9"
case zero = "0"
case moltiplicazione = "X"
case divisione = "/"
case somma = "+"
case meno = "-"
case uguale = "="
case AC = "AC"
case piùMeno = "±"
case percentuale = "%"
case virgola = "."
case niente = ""
}
struct ContentView: View {
var numeri: [[Nums]] = [
[Nums.AC, Nums.piùMeno, Nums.percentuale, Nums.divisione],
[Nums.sette, Nums.otto, Nums.nove, Nums.moltiplicazione],
[Nums.quattro, Nums.cinque, Nums.sei, Nums.meno],
[Nums.uno, Nums.due, Nums.tre, Nums.somma],
[Nums.zero, Nums.niente, Nums.virgola, Nums.uguale]
]
private var gridItemLayout = [GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())]
@State var res: Double = 0
@State var previousVar: Double = 0
let timer = Timer.publish(every: 0.001, on: .main, in: .common).autoconnect()
@State var con: Int = 0
@State var final: Double = 0
@State var operat = Nums(rawValue: "")
var body: some View {
VStack{
Spacer()
HStack{
Spacer()
Text("\(res, specifier: "%.2f")")
.font(.system(size: 50))
.bold()
.padding()
}
Spacer()
LazyVGrid(columns: gridItemLayout, content: {
ForEach(0..<5){ i in
ForEach(0..<4){ j in
RoundedRectangle(cornerRadius: 35.0)
.foregroundColor(.orange)
.frame(width: 80, height: 80, alignment: .center)
.overlay(
Text("\(numeri[i][j].rawValue)")
.font(.largeTitle)
.foregroundColor(.black)
).onTapGesture {
switch numeri[i][j] {
case Nums.AC:
operat = Nums.AC;
res = 0
case Nums.uguale:
operat = Nums.uguale;
res = final
case Nums.somma:
operat = Nums.somma;
previousVar = res;
res = 0;
con = 0
case Nums.meno:
operat = Nums.meno;
previousVar = res;
res = 0
con = 0
case Nums.divisione:
operat = Nums.divisione;
previousVar = res;
res = 0;
con = 0
case Nums.moltiplicazione:
operat = Nums.moltiplicazione;
previousVar = res;
res = 0;
con = 0
case Nums.percentuale:
operat = Nums.percentuale;
res = res / 100
case Nums.piùMeno:
operat = Nums.piùMeno;
res = res * -1;
con = 0
case Nums.virgola:
operat = Nums.virgola;
res = Double(String(res) + ".") ?? 0
default:
res = Double(numeri[i][j].rawValue) ?? 0
con += 1
}
}
}
}
}).onReceive(timer) { _ in
if con != 0 {
if operat == Nums.divisione{
final = previousVar / res
} else if operat == Nums.meno{
final = previousVar - res
} else if operat == Nums.moltiplicazione{
final = previousVar * res
} else if operat == Nums.somma{
final = previousVar + res
}
}
}
}.padding(2)
}
}


At line 115 I am making res equal a number, what I want to do is to append that number to res
Thank you
Why do you work with numbers and not Strings, and make conversion after tapping ?
Accepted Answer
As already suggested, you should better hold the String representation of displayed digits and the Double value of it separately, but mutually dependent.

In your case, computed property will work:
Code Block
struct ContentView: View {
//...
private var gridItemLayout = [GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())]
//@State var res: Double = 0
@State var digits: String = "0"
var res: Double {
get {
digits.hasSuffix(".")
? Double(digits.dropLast()) ?? 0.0
: Double(digits) ?? 0.0
}
nonmutating set {
digits = String(format: "%.10g", newValue) //<- May need better formatting
}
}
@State var previousVar: Double = 0
//...
var body: some View {
VStack{
Spacer()
HStack{
Spacer()
Text("\(digits)") //<-
.font(.system(size: 50))
.bold()
.padding()
}
//...
switch numeri[i][j] {
case Nums.AC:
operat = Nums.AC;
res = 0
case Nums.uguale:
operat = Nums.uguale;
res = final
case Nums.somma:
operat = Nums.somma;
previousVar = res;
res = 0
con = 0
case Nums.meno:
operat = Nums.meno;
previousVar = res;
res = 0
con = 0
case Nums.divisione:
operat = Nums.divisione;
previousVar = res;
res = 0
con = 0
case Nums.moltiplicazione:
operat = Nums.moltiplicazione;
previousVar = res;
res = 0
con = 0
case Nums.percentuale:
operat = Nums.percentuale;
res = res / 100
case Nums.piùMeno:
operat = Nums.piùMeno;
//res = res * -1;
if digits.hasPrefix("-") {
digits = String(digits.dropFirst())
} else {
digits = "-" + digits
}
con = 0
case Nums.virgola:
operat = Nums.virgola;
//res = Double(String(res) + ".") ?? 0
if !digits.contains(".") {
digits += "."
}
default:
//res = Double(numeri[i][j].rawValue) ?? 0
if digits == "0" {
digits = numeri[i][j].rawValue
} else {
digits += numeri[i][j].rawValue
}
con += 1
}
//...
}.padding(2)
}
}


You may need to improve many parts, but with this, you can append that number to res.
Append to Double
 
 
Q