Rounding Floats (0.1 Rounds)
#1

pawn Код:
new valueA;
valueA=7;
new Float:floatA=valueA;
floatA/=10.0; // Results as 0.699999 instead of 0.700000
printf("%.1f",floatA);
The print should shows 0.7 (0.700000), but instead shows 0.6 (0.699999)
Why sometimes floats don't divide correctly is beyond me, but my question is:
How would I round that 0.699999 to 0.700000 so I'll be able to display it as 0.7 instead of 0.6?

I've searched the forum thus how I came across learning to only display 0.7 instead of 0.700000, but nothing about rounding that .1 to make it 0.7
The Wiki explains only the basics of rounding up floats (Whole Numbers), but nothing about what I'm asking for... Unless I missed something
So if you know of a post/page that would explain how this is all done, I would appreicate a link, thanx for reading
Reply
#2

Use floatround(Float:value, method) and floatround_round for method.

EDIT*
Hmm, I think I see what you mean.

pawn Код:
Float:RoundToNearestTenth(Float:value)
{
  new Float:tmp=value*10;
  return floatdiv(floatround(tmp,floatround_round),10);
}
NOTE: In formatting, use '%.01f' so that a number follows the decimal at all times, even if that number is 0
Reply
#3

Alright, thanx, I'll give it a shot, but thought that would round the entire float (1.0)

Anyways, I'll post the results when done
Reply
#4

pawn Код:
floatround(floatA,floatround_ceil);
The actual number that was used was: 7
It was turned to a float just like the example code in the first post (I will actually update the number(s) there too)
When divided by 10.0, it came up as 0.699999
Then I used the above floatround method which did nothing, it remained: 0.699999

I had a feeling that wouldn't work, but actually thought it would've rounded it up to 1.0

I might have screwed up the first post as a matter of fact, gonna update it though
EDIT: Nah, used a right number example, but only to change number to 0.699999 etc.

NEW EDIT: Alright, updated the values actually used to make better understanding of my problem here... No idea how to round that 699999 to 700000
Reply
#5

Did you use the function I gave you? It worked fine for me.

Example:
pawn Код:
#include <a_samp>
Float:RoundToNearestTenth(Float:value)
{
  new Float:tmp=value*10;
  return floatdiv(floatround(tmp,floatround_round),10);
}
public OnFilterScriptInit()
{
    printf("%.01f",RoundToNearestTenth(13.29));
    return 1;
}
returned 13.3, as it should have.
Reply
#6

Quote:
Originally Posted by Joe Staff
pawn Код:
Float:RoundToNearestTenth(Float:value)
{
  new Float:tmp=value*10;
  return floatdiv(floatround(tmp,floatround_round),10);
}
Yes, I gave your function a shot, but resulted in the rounding the whole number, 0.699999 was rounded to 10.0 for example, but I might have messed it up on my part, so I'll test again later on. I haven't even noticed your last post, so I'll also give that a shot too when I get the chance. But I think that floatdiv is the key, didn't even think of using that, so thanx Joe

I'll probably be able to figured this out, just need more time to work with it :P
But for now, I gotta run (Life Calls Me)... IF by chance I still can't get it to work though, I'll post again

NOTE: Some floats when divided by 10 come out alright [value 6] (0.600000), but other times come out as [value 7] (0.699999 when it should've been 0.700000)
But the method used was floatA/=10 or floatA/=10.0 and NOT floatdiv...
Reply
#7

Alright, took a little time to do a bit more testing with your function, but it failed to round value 7 (0.699999 to 0.700000), but values 0 to 6 work just fine...

Using the value 9 does the same thing, it comes out as 0.899999

The following is how I used your function though

pawn Код:
valueA=7;
floatA=valueA;
// floatA is now: 7.000000
floatA/=10.0;
// floatA is now: 0.699999 (It's not 0.700000 like it should be after the divide...)
RoundToNearestTenth(floatA);
// floatA is still: 0.699999
I tested the number you used, 13.29 -> 13.3 which works just fine, but try with using 0.69 & you'll see what my problem is...

Conclusion: This must be a Pawn Bug of some sort, 7.000000 / 10.000000 = 0.699999 (Pawn) because 7.000000 / 10.000000 = 0.700000 (Caculator)

So does anyone know a way around this bug maybe?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)