improve math.digit and add math.count_digit
parent
ca40cf3eb6
commit
1e607656ed
|
|
@ -79,7 +79,7 @@ pub fn digits(_n i64, _b ...int) []int {
|
||||||
// count_digits return the number of digits in the number passed.
|
// count_digits return the number of digits in the number passed.
|
||||||
// Number argument accepts any integer type (i8|i16|int|isize|i64) and will be cast to i64
|
// Number argument accepts any integer type (i8|i16|int|isize|i64) and will be cast to i64
|
||||||
pub fn count_digits(n i64) int {
|
pub fn count_digits(n i64) int {
|
||||||
return int(math.ceil(math.log(f64(n) + 1 ) / math.log(10.0)))
|
return int(math.ceil(math.log(math.abs(f64(_n) + 1 )) / math.log(10.0)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// minmax returns the minimum and maximum value of the two provided.
|
// minmax returns the minimum and maximum value of the two provided.
|
||||||
|
|
|
||||||
|
|
@ -982,3 +982,9 @@ fn test_powi() {
|
||||||
assert powi(0, -2) == -1 // div by 0
|
assert powi(0, -2) == -1 // div by 0
|
||||||
assert powi(2, -1) == 0
|
assert powi(2, -1) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_count_digits() {
|
||||||
|
assert count_digits(12345) == 5
|
||||||
|
assert count_digits(i64(1234567890)) == 10
|
||||||
|
assert count_digits(-67345) == 5
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue