information from the ultrasonic sensor

I get information from the ultrasonic sensor The distance values from the 5 to 200 cm If I want to output to 4 digits, how to deal with

For example 5 ---> 0005 15 ---> 0015 150 ---> 0150

You could use sprintf (buf, "%04d", x);, or you could do it explicitly by testing to see is "x" is less than 1000, 100, 10 and printing the appropriate number of leading zeroes.

Why do you want to output to 4 digits? Surely you don't expect to measure distances of 1000 cm or more, do you?