Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if number is decimal with swift

Tags:

ios

swift

How can i check if a number is decimal or not using swift?

With Objective-C:

if (number == (int) number) {
  //decimal
}
else {
  //not decimal
}
like image 504
Mono.WTF Avatar asked Aug 28 '14 15:08

Mono.WTF


People also ask

How do you check if a number is decimal number or not?

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.

How do you check if a number is an integer in Swift?

main.swift var x = 25 if x is Int { print("The variable is an Int.") } else { print("The variable is not an Int.") }

How do you check string is number or not in IOS Swift?

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.

Which data type will allow decimal numbers?

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.


1 Answers

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.)

like image 166
Jessy Avatar answered Sep 30 '22 18:09

Jessy