Hello,
I have this program where I have to do some calculations,which is no problem but the results should be one decimal place. For Example, if I have 9.09090901 I want it to be printed as 9.1. By any chance is there anyone can tell me how to do this or give some advice.Any help will be greatly appreciated.
Helios,
Thanks for the info, but it doesn't cover how to round a double one decimal place. It talks about rouding a double to an int. By any chance do you know how to round a double to double but one decimal place.
I am a new member and this is my first post. Please excuse me iIf I am repeating anything posted here or elsewhere. While the original post in this thread did say that the rounded results are to be printed rounded to one decimal place there is a disctintion if one simply wants the rounded data to be printed or carried forward to other calculations. In one case the printf format arguments will work well while in the other case a function like that posted by helios and buffbill or discussed in article 3638 may be required. One caution I would like to add is how thr round-up or round-down rule is applied to results like X.5. Either rule will build in an error with large amount of data if the rounded data is used. If this is a potential problem perhaps a counter and a modulus operation the counter to alternate between round-up and the round-down rules could be considered. Regards.
@buffbill
i m sorry but thr is a slight flaw in the code u gave.
for a number say 7.13, it'll return 7.2 instead of 7.1.so i propose a correction in ur code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
float rndup(float n)//round up a float type and show one decimal place
{
float t;
t=n-floor(n);
if (t>=0.5)
{
n*=10;//where n is the multi-decimal float
ceil(n);
n/=10;
}
else
{
n*=10;//where n is the multi-decimal float
floor(n);
n/=10;
}
return n;
}