1
I’m not getting round the float to the first two decimal places.
I wrote my program like this:
float valor;
scanf("%f", &valor);
printf("valor = %f\n", valor);
Once compiled, I typed 573.93 to test, and what returned was:
valor = 573.929993
I tried to use the function round() library Math. h,but it didn’t work either. I used it like this:
float valor;
scanf("%f", &valor);
printf("1° valor = %f\n", valor);//TESTE
valor = round(valor*100.0)/100.0;
printf("2° valor = %f\n", valor);//TESTE
And this returned:
1° valor = 573.929993
2° valor = 573.929993
Also used so:
float valor;
scanf("%f", &valor);
printf("1° valor = %f\n", valor);//TESTE
valor = valor * 10.0;
printf("2° valor = %f\n", valor);//TESTE
valor = valor * 10.0;
printf("3° valor = %f\n", valor);//TESTE
valor = round(valor);
printf("4° valor = %f\n", valor);//TESTE
valor = valor/100.0;
printf("5° valor = %f\n", valor);//TESTE
And what returned was:
1° valor = 573.929993
2° valor = 5739.299805
3° valor = 57393.000000
4° valor = 57393.000000
5° valor = 573.929993
Can someone help me round up the float for value = 573.93?
See then what you did is what you can do, but the problem is to use
float
when accuracy is desired, it does not provide this. Never use binary floating point for money.– Maniero
If it is just printing accuracy, you ask for it:
printf("%.0f\n", valor)
; will print with no box after the dot, in the same way as . 1 will print a box after the dot, so on.– Gabriel Pellegrino