0
I know I can get a limit of digits after the comma with %.2f
for float
and %.2lf
for double
.
How can I change this limit depending on the occasion?
For example:
Get a two-digit limit with float
that would be %.2f
, that is, if the number is 310.22
, the same will appear - but, if the number ends at zero, decrease this limit: in the case of 310.10
, would be 310.1
(and the numbers will vary).
When compiling the programming below with 16
and 455
the result will be 436.10
, but I want to show you how 436.1
, without modifying the other results which, if they do not end with zero, have two decimal places.
#include<stdio.h>
int main()
{
double d,km,km2,v,k;
scanf("%lf%lf",&d,&km);
v=d*30;
km2=km*0.01;
v=v+km2;
k=v*0.10;
v=v-k;
printf("\n%.2lf",v);
return 0;
}
Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).
– Maniero