# Thread: Decimal place range. simple help needed

1. ## Decimal place range. simple help needed

How do i make the program to print Sorry... if user enter a 4 decimal place number???

Code:
```# include <stdio.h>

main()
{
float length;
float cm;
float m;
float km;
float inch;
float yard;
float mile;

printf("Metric-Imperial Conversion\n\n");  /* This is the program name */

printf("This program allows values up to 3 decimal place\n");
printf("Get Length: ");    /*user input values to be convertered*/
scanf("&#37;f" , &length);

if ( length < 0 )    /*How do i make the program to print Sorry... if user enter a 4 decimal place number???*/
{

printf("\aSorry, Pls re-enter value \n");

}

else {

cm = length * 2.5400;
m = length * 0.91440;
km = length * 1.60934;

printf("\n\nFrom %.3f Inch to CM: %.3f \n", length, cm);
printf("From %.3f Yard to Metres: %.3f \n", length, m);
printf("From %.3f Miles to KM: %.3f \n\n\n", length, km);

inch = length * 0.393700;
yard = length * 1.093613;
mile = length * 0.621371;

printf("From %.3f CM to Inch: %.3f \n",length, inch);
printf("From %.3f Meters to Yard: %.3f \n",length, yard);
printf("From %.3f KM to Mile: %.3f \n\n", length, mile);

printf("Have a Good Day!!\n");
}
}```

2. By 4 decimal place number, do you mean 1000 or do you mean 0.0001? If the first, that's pretty straightforward. If the second, that's not.

3. Multiply the user's value by 1000.0, convert to an int, convert back to float, and then divide by 1000.0.

4. Originally Posted by tabstop
By 4 decimal place number, do you mean 1000 or do you mean 0.0001? If the first, that's pretty straightforward. If the second, that's not.
hi,

i mean some thing like the 0.0001. If user inputed this values, can it print the sorry statement?

5. Originally Posted by Dino
Multiply the user's value by 1000.0, convert to an int, convert back to float, and then divide by 1000.0.
hi dino,

i am sorry but i don't get you.. why is there a need to *1000 convet to int and back to float then divide again?

6. Originally Posted by Chris0724
hi dino,

i am sorry but i don't get you.. why is there a need to *1000 convet to int and back to float then divide again?
Because you want to round off/truncate at the third decimal point.

BTW: It is not possible to store (say) 0.001 precisely inside a computer even if you wanted to (unless you're using BCD, but you probably aren't), so this whole thing is somewhat irrelevant. If you're worried about how things print, just use print formats to control how many decimals are printed.

7. ok.. thanks a lot for the advice

8. Another option is to read in a character string instead of a float, parse the value to validate it for no more than 3 decimal places, and then use a function like sscanf(), then convert to float if the number is valid.

9. Originally Posted by Dino
Another option is to read in a character string instead of a float, parse the value to validate it for no more than 3 decimal places, and then use a function like sscanf(), then convert to float if the number is valid.
That, in my mind is the only thing you can do.

However, what is the point of doing that. If the user enters 0.00001, then what happens? It just outputs 0.000 - which is the approximate distance in the new measure.

--
Mats