How can i check if a number is decimal or not using swift?
With Objective-C:
if (number == (int) number) {
//decimal
}
else {
//not decimal
}
function number_test(n) { var result = (n - Math. floor(n)) !== 0; if (result) return 'Number has a decimal place. '; else return 'It is a whole number.
main.swift var x = 25 if x is Int { print("The variable is an Int.") } else { print("The variable is not an Int.") }
String represents a numberSwift Character has a built-in property, isNumber , that can indicate whether the character represents a number or not. We use this property along with allSatisfy to check whether all the characters in a string are a number.
The Decimal data type provides the greatest number of significant digits for a number. It supports up to 29 significant digits and can represent values in excess of 7.9228 x 10^28. It is particularly suitable for calculations, such as financial, that require a large number of digits but cannot tolerate rounding errors.
extension FloatingPoint {
var isInteger: Bool { rounded() == self }
}
(You'll also need to use the return
keyword if you're not using Swift 5.1 or later.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With