如何在swift中逐步增加或减少一个双值



假设我们有一个双值0.000025900

预期:调用minus()返回0.000025899plus()返回0.000025901

到目前为止我尝试过的:

func plus(text: String) -> String {
let reminder = text.components(separatedBy: ".").last ?? "0"
let integer = text.components(separatedBy: ".").first ?? "0"
let str = (Int(reminder) ?? 0) + 1
return String(integer) + "." + String(str)
}
func minus(text: String) -> String {
let reminder = text.components(separatedBy: ".").last ?? "0"
let integer = text.components(separatedBy: ".").first ?? "0"
let str = max(0, (Int(reminder) ?? 0) - 1)
return String(integer) + "." + String(str)
}

这两个函数只有在.0.25900等截断部分之间没有0s时才能很好地工作。

您的代码适合第一部分,即将整数与小数部分分开。为了在函数返回时保持小数部分的正确格式,您需要将字符数存储在变量中,并使用它将其转换回。

以下是plus()函数的代码:

func plus(text: String) -> String {
let remainder = text.components(separatedBy: ".").last ?? "0"
let integer = text.components(separatedBy: ".").first ?? "0"

// Store the number of characters of the remainder
let length = remainder.count

let newRemainder = (Double(remainder) ?? 0.0) + 1.0

// Turn the "integer remainder" into a Double using the number of characters to calculate the denominator
let result = (Double(integer) ?? 0.0) + newRemainder / pow(10, Double(length))

// Format the result using the number of characters as the precision
return String(result.formatted(.number.precision(.fractionLength(length))))
}

我仍然需要弄清楚minus()函数的代码…:(

这个问题可能有很多解决方案。这是我的解决方案。可能存在一些错误。我认为它可以帮助你如何执行负函数。

func plus(text: String) -> String {
guard text.contains(".")  else {
// value = 2 return value should be 2.1
return text + ".1"
}
let intialValueAfterDecimalPoint = text.components(separatedBy: ".").last ?? "0"
// Make .00 to .100 / .9001 to .19001 if at some point only 0's after decimal point for making easy calculation
let modifiedIntialValueAfterDecimalPoint = "1" + intialValueAfterDecimalPoint
let intialIntegerValue = text.components(separatedBy: ".").first ?? "0"
let modifiedIntitialValueAfterDecimalPoint = Int(modifiedIntialValueAfterDecimalPoint) ?? 0
//Need to add carry value to initial interger value if there is a carry value in decimalValue addition
// For example .99 + 1 = 1.00
let firstIndexValueWithCarryValue = calculateCarryValue(text: String(modifiedIntitialValueAfterDecimalPoint + 1))

if firstIndexValueWithCarryValue == 2 {
let finalValueAfterDecimalPoint = String(modifiedIntitialValueAfterDecimalPoint + 1).dropFirst()
return String((Int(intialIntegerValue) ?? 0) + 1) + "." + finalValueAfterDecimalPoint
} else {
return String(intialIntegerValue) + "." + String(modifiedIntitialValueAfterDecimalPoint + 1).dropFirst()
}
}
func calculateCarryValue(text: String) -> Int {
let revString = String(text.reversed())
let carryValue = (Int(revString) ?? 0) % 10
return  carryValue
}
func minus(text: String) -> String {
guard text.contains(".")  else {
// value = 2 return value should be 1.9
let intValue = Int(text) ?? 0
if intValue == 0 {
return "-1"
} else  {
return String(intValue - 1) + ".9"
}
}
let intialValueAfterDecimalPoint = text.components(separatedBy: ".").last ?? "0"
// Make .00 to .100 / .9001 to .19001 if at some point only 0's after decimal point for making easy calculation
let modifiedIntialValueAfterDecimalPoint = "1" + intialValueAfterDecimalPoint
let intialIntegerValue = text.components(separatedBy: ".").first ?? "0"
let modifiedIntitialValueAfterDecimalPoint = Int(modifiedIntialValueAfterDecimalPoint) ?? 0
//Need to minus carry value to initial interger value if there is a carry value in decimalValue subtraction
// For example 1.000 - 1 = 0.999
let firstIndexValueWithMinusCarryValue = String(modifiedIntitialValueAfterDecimalPoint - 1)
let negativeCarryValue = firstIndexValueWithMinusCarryValue.count < modifiedIntialValueAfterDecimalPoint.count ? 1 : 0

if negativeCarryValue == 1 {
let finalValueAfterDecimalPoint = String(modifiedIntitialValueAfterDecimalPoint - 1)
return String((Int(intialIntegerValue) ?? 0) - 1) + "." + finalValueAfterDecimalPoint
} else {
return String(intialIntegerValue) + "." + String(modifiedIntitialValueAfterDecimalPoint - 1).dropFirst()
}
}
print(plus(text: "1.00"))
print(plus(text: "0.9999"))
print(minus(text: "1.00"))
print(minus(text: "0.9999"))

只需使用DecimalnextUpnextDown

最新更新