Raised This Month: $ Target: $400
 0% 

Float problem


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
reinert
Veteran Member
Join Date: Feb 2007
Old 05-07-2011 , 06:46   Float problem
Reply With Quote #1

Hey, Why do I get round float ?

I've code:

PHP Code:
new 6436
new 1544

get_float_ab
()
{
    return 
float:( a/1.0 );

Then I'm printing it as a message:
PHP Code:
client_print(0print_chat"a / b = %0.1f"get_float_ab() ) 
And it prints:

a / b = 4.0

As the result should be 6436 / 1544 = 4.1 ( 4.168... )
reinert is offline
Hunter-Digital
Veteran Member
Join Date: Aug 2006
Location: In the Game [ro]
Old 05-07-2011 , 07:06   Re: Float problem
Reply With Quote #2

Your stock needs a float tag and its case sensitive, so it MUST be "Float".

EDIT: this seems to work, returns 4.168393
Code:
Float:get_float_ab()
{
    return (a + 0.0) / (b + 0.0)
}
EDIT #2: It works (a / b = 4.168393) even without Float: tag but it gives out a tag mismatch on compile.
__________________

Last edited by Hunter-Digital; 05-07-2011 at 07:17.
Hunter-Digital is offline
reinert
Veteran Member
Join Date: Feb 2007
Old 05-07-2011 , 07:15   Re: Float problem
Reply With Quote #3

ok, but it still returns me round number, 4.0 instead of 4.1.
reinert is offline
Hunter-Digital
Veteran Member
Join Date: Aug 2006
Location: In the Game [ro]
Old 05-07-2011 , 07:16   Re: Float problem
Reply With Quote #4

I dunno what version of my post you saw, read again 'cause it works now.
__________________
Hunter-Digital is offline
reinert
Veteran Member
Join Date: Feb 2007
Old 05-07-2011 , 07:24   Re: Float problem
Reply With Quote #5

Ok thanks, now it works. one more question but it's not so necessary, How can I round number from 4.16 to 4.2 ?

Round the second number after dot.
reinert is offline
Hunter-Digital
Veteran Member
Join Date: Aug 2006
Location: In the Game [ro]
Old 05-07-2011 , 07:33   Re: Float problem
Reply With Quote #6

If you're only using it for printing, you can just use %.1f and it will round 4.168393 to 4.2.

But for actual stored rounded value, I dunno :/
__________________
Hunter-Digital is offline
Elusive138
Senior Member
Join Date: Dec 2010
Old 05-07-2011 , 07:40   Re: Float problem
Reply With Quote #7

There might be an easier way, but for rounding to a number of decimal places, you can always do
PHP Code:
fNum floatround(fNum*10) / 10.0 
PHP Code:
stock Float:floatround_dp(Float:numdp)
{
     new 
Float:mult floatpower10.0float(dp) )
     return 
floatroundnum mult ) / mult

All that does is shift everything you want to keep left of the decimal point, round it, and shift it back.

Last edited by Elusive138; 05-07-2011 at 07:56.
Elusive138 is offline
reinert
Veteran Member
Join Date: Feb 2007
Old 05-07-2011 , 07:49   Re: Float problem
Reply With Quote #8

PHP Code:
return floatdiv(ab
Gives me 2 tag mismatch errors.
reinert is offline
grimvh2
Veteran Member
Join Date: Nov 2007
Location: Fishdot Nation
Old 05-07-2011 , 07:50   Re: Float problem
Reply With Quote #9

a and b arent floats
__________________
I am out of order!
grimvh2 is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 05-07-2011 , 09:20   Re: Float problem
Reply With Quote #10

Code:
return floatdiv(float(a), float(b))
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 04:17.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode