我需要类似的东西
var a = 1
var b = 2
var c = a + b should be 12 (Int) instead of 3.
var a = 12
var b = 34
var c = a + b should be 1234 (Int) instead of 46.
我不知道我们怎么能做到这一点?
一种方法是将两个Int都转换为String,然后再次将String转换为Int,然后再对其进行隐蔽,但是我认为这样做并不高效。
如果有解决方案,请先谢谢您。
答案 0 :(得分:2)
12 + 34 = 12 * 10 ^ 2 + 34 = 1200 + 34 = 1234
func logC(val: Double, forBase base: Double) -> Double {
return log(val)/log(base)
}
var a = 10
var b = 0
let x = b == 10 ? 2 : b == 0 ? 1 : ceil(logC(val: Double(b), forBase: 10))
var c = Int(Double(a) * pow(10, x) + Double(b))
print(c)
答案 1 :(得分:1)
您可以这样写:
extension Int {
func concatenateDecimalDigits(in other: Int) -> Int {
let scale: Int
switch other {
case 0...9:
scale = 10
case 10...99:
scale = 100
case 100...999:
scale = 1000
case 1000...9999:
scale = 10000
case 10000...99999:
scale = 100000
case 100000...999999:
scale = 1000000
//You need to add more cases if you need...
//...
//...
default:
scale = 0 //ignore invalid values
}
return self * scale + other
}
}
var a = 1
var b = 2
print( a.concatenateDecimalDigits(in: b) ) //->12
a = 12
b = 34
print( a.concatenateDecimalDigits(in: b) ) //->1234
a = 122344
b = 9022
print( a.concatenateDecimalDigits(in: b) ) //->1223449022
您可以编写一些逻辑来计算scale
,而无需switch
,但这并没有太大区别。
答案 2 :(得分:0)
任意两个Int
的常规解决方案:
您需要计算位数以计算乘数:
func numberOfDigits(_ num: Int) -> Int {
var count = 0
var number = num
while number > 0 {
number = number / 10
count += 1
}
return count
}
用作:
let a = 11
let b = 24
let noOfDigit = numberOfDigits(b)
let multiplier = pow(Double(10), Double(noOfDigit))
let c = a * Int(multiplier) + b
print(c)
并且,在一行中为:
let c = a * Int(pow(Double(10), Double(numberOfDigits(b)))) + b
答案 3 :(得分:0)
您可以通过以下简单的转换来做到这一点:
version: "3.3"
services:
test-backend:
image: test-002
container_name: test-002
restart: always
volumes:
- ./assets:/opt/test_files
- ./medialibrary:/opt/test_medialibrary
- ./app-configs:/opt/test_configs
- ./logs/backend:/opt/test-backend-logs
extra_hosts:
test-converter: "127.0.0.1"
ports:
- "8000:8000"
volumes:
test-data:
test-data2: