number float - nberlette/is GitHub Wiki
function isFloat<N = number>(it: N): it is Float<N>;
Checks if the value is a floating-point number. Supports both single and double precision floating-point numbers.
Name | Info |
---|---|
it |
The value to check. |
true
if the value is a floating-point number, false
otherwise.
Numbers
import { isFloat } from "@nick/is/float";
isFloat(1); // false
isFloat(1.0); // false
isFloat(1.1); // true
isFloat(1.00001e1); // true
function isFloat(it: unknown): it is Float;
Checks if the value is a floating-point number. Supports both single and double precision floating-point numbers.
-
0
,-0
are considered valid floating-point numbers, and returntrue
. -
1
,-1
and other integers (or "non-floating" values) returnfalse
. - Special cases like
Infinity
,-Infinity
, andNaN
returnfalse
.
Name | Info |
---|---|
it |
The value to check. |
true
if the value is a floating-point number, false
otherwise.
Numbers
import { isFloat } from "@nick/is/float";
isFloat(0); // true
isFloat(1); // false
isFloat(1.0); // false
isFloat(1.1); // true
isFloat(1.00001e1); // true
export type Float<N = number> = Cast<N, FLOAT>;
Casts a value into a floating-point type. If the value is not a number, it will
resolve to never
.
-
N
(default:number
)
Numbers
Types
float
number
import { type Float, isFloat } from "@nick/is/float";
let x = 1.5 as Float, y = 0;
if (isFloat(x)) {
console.log(x);
} else {
console.log(y);
}
// This will raise a TypeScript compiler error:
x = 1; // <- TS2322 Type '1' is not assignable to type 'Float'.
export type MaybeFloat<N = number> = Cast<N, MAYBE_FLOAT>;
Casts a value into a partial floating-point type. If the value is not a number,
it will resolve to never
.
-
N
(default:number
)
Numbers
Types
maybe
float
number
import { isFloat, type MaybeFloat } from "@nick/is/float";
let x = 1.5 as MaybeFloat, y = 0;
if (isFloat(x)) {
console.log(x);
} else {
console.log(y);
}
y = 1; // <- No error! (this is the main difference from `Float`)