This is my updated answer (thanks to Martin R and the OP for the remarks). The OP's problem was just casting the `pow(x: Decimal,y: Int) -> Decimal` function to an Int after subtracting 1 from the result. I have answered the question with the help of this SO post for NSDecimal and Apple's documentation on Decimal. You have to convert your result to an NSDecimalNumber, which can in turn be casted into an Int:

``````let size = Decimal(2)
let test = pow(size, 2) - 1
let result = NSDecimalNumber(decimal: test)
print(Int(result)) // testing the cast to Int
``````

### python 小数转整数_swift字典转模型_string转int-华为云

https://www.huaweicloud.com/term/topic-1737296_2.html

### Swift 3 : Decimal to Int - Stack Overflow

https://stackoverflow.com/questions/39731265/swift-3-decimal-to-int

This is my updated answer (thanks to Martin R and the OP for the remarks). The OP's problem was just casting the pow(x: Decimal,y: Int) ...

### Swift - 浮点数转换成整数（四舍五入与直接截断）_winter's blogs ...

https://blog.csdn.net/wint_ing/article/details/81518066

2016年11月8日 ... var totalCost = lround(mead + mead * Double(tip) / 100.0 + mead * Double(tax) / 100.0)1，直接截去小数部分转换整数使用强制转换会将浮点 ...

### swift 向下取整和向上取整函数- 简书

https://www.jianshu.com/p/0312918ec7cf

2017年9月7日 ... 主要就是这几个函数根据需要，用floor, ceil,或者round ceil(x)返回不小于x的最小 整数值（然后转换为double型）。 floor(x)返回不大于x的最大...

### Decimal to Double? - Using Swift - Swift Forums

https://forums.swift.org/t/decimal-to-double/4600

Jan 18, 2018 ... public init(floatLiteral value: Double) { self.init(value) } }. extension Decimal : ExpressibleByIntegerLiteral { public init(integerLiteral value: Int) {

### Swift入门（九）——String与Int、Double、Float等数字相互转换_KT ...

https://blog.csdn.net/abc649395594/article/details/47628021

2015年8月14日 ... 一、String转数字这里以String类型转Int类型为例。 ... 在Swift里面，Int、float、 double等数字类型自成一派，但它们和String类型之间的转换总是 ...

### 如何在Swift中获取数字的整数部分和小数部分- Thinbug

https://www.thinbug.com/q/44909637

### iOS 向下取整、向上取整、四舍五入- 紫洁- 博客园

https://www.cnblogs.com/muzijie/p/6489149.html

2017年3月2日 ... 向上取整：float ceilf(float); double ceil(double); 向下取整：float floorf(float); double floor(double); 四舍五入：f.

### How to use pow() in Swift 3 and get an Int - Stack Overflow

https://stackoverflow.com/questions/42338009/how-to-use-pow-in-swift-3-and-get-an-int

Int does not have an initializer taking Decimal , so you cannot convert Decimal to Int directly with an initializer syntax like Int(...) . You can use ...

### Decimal | Apple Developer Documentation

https://developer.apple.com/documentation/foundation/decimal

Creates a new decimal value exactly representing the provided integer. init(Int). Creates and initializes a decimal with the provided integer value.

### Swift5.1-NumberFormatter强大的格式化数字函数（iOS，MacOS等 ...

https://zhuanlan.zhihu.com/p/77799920

2019年8月12日 ... 现实开发中经常会遇到把数字转化为我们想要的数字格式，再转化为字符串 ... 因为 数字 有整数小数啊等等，所以在正式操作之前，需要统一把 ...

### How to get the integer part and fractional part of a number in Swift ...

https://stackoverflow.com/questions/44909637/how-to-get-the-integer-part-and-fractional-part-of-a-number-in-swift

You could do simple floor and truncating: let value = 1234.56789 let double = floor(value) // 1234.0 let integer = Int(double) // 1234 let decimal ...

### How to convert an int to a float - free Swift 5.1 example code and tips

https://www.hackingwithswift.com/example-code/language/how-to-convert-an-int-to-a-float

May 28, 2019 ... Swift's Float data type has a built-in constructor that can convert from integers with no extra work from you. For example, to convert the integer ...

### The Basics — The Swift Programming Language (Swift 5.4)

https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html

Swift is a new programming language for iOS, macOS, watchOS, and tvOS app development. ... All of these integer literals have a decimal value of 17 :.

### Swift Integer Quick Guide

https://useyourloaf.com/blog/swift-integer-quick-guide/

Feb 20, 2017 ... So here is my quick guide to Swift integers. Last Updated: Jan 16, ... You are not restricted to decimal for integer literals. You also have binary ...

### Formatting numbers in Swift | Swift by Sundell

https://www.swiftbysundell.com/articles/formatting-numbers-in-swift

Nov 1, 2020 ... For example, rather than simply converting its Double into a String , we could use a custom format to round it to two decimal places, which will ...