tell me round off a real number in 2nd decimal place: input-> output; 7.632342534245 -> 7.60; 8.85 -> 8.90; 3.96 -> 4.00 i wrote below code which is working but my problem is if it more than two decimal value i have to change every time the 10 to 100. so tell me how can i solve this problem. Code: #include <stdio.h> int main() { //int *precison=3; float bill_amount,m,a,d; printf("enter the bill amount "); scanf("%f",&bill_amount); printf("entered bill amount:%f\n",bill_amount); m=bill_amount*10; a=m+0.5; d=a/10; printf("m=%.2f\n",m); printf("a=%.2f\n",a); printf("d=%.2f\n",d); }
>>my problem is if it more than two decimal value i have to change every time the 10 to 100 Why is that a problem? However you do it, you're going to have to work out how much to remove from the number, if it's 7.632342534 to 1dp then 0.032342534, if 2dp then 0.002342534, etc. And the easiest way to do that is to multiply it by 10, 100 etc, remove everything right of the dot then divide it again, which is what you're doing. If the problem is that you don't know how many dp until runtime, then extend the code to calculate the factor by which the number must be multiplied or divided, i.e. if they say 2 dp, then 10*10, if 3 then 10*10*10, etc - I'm sure you can work out how to do that in a for loop.