Answers for "Count the digits in an integer"

5

find number of digits in a number

floor(log10(n) + 1);
Posted by: Guest on February-20-2021
0

get number of digits in a number

function getlength(number) {
    return number.toString().length;
}
Posted by: Guest on May-11-2021
0

Count the digits in an integer

// Count the digits in an integer
fn numlen(n: u64) -> (u64, usize, usize) {

// Method 1: Calculate
	let length1 = ((n as f32).log10()) as usize + 1;

// Method 2: Convert to string
	let n_str = n.to_string();
	let length2 = n_str.len() as usize;
	(n, length1, length2)
}

fn main() {
    println!("length n = {:?} ", numlen(123456789));
    println!("length n = {:?} ", numlen(1234567891));
}
Posted by: Guest on October-12-2021

Code answers related to "Count the digits in an integer"

Browse Popular Code Answers by Language