Raised This Month: $51 Target: $400
 12% 

StringToFloat gives 0.3499999


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
eyal282
Veteran Member
Join Date: Aug 2011
Old 08-27-2018 , 05:30   StringToFloat gives 0.3499999
Reply With Quote #1

How to fix such thing? Any better function I should know of?
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334
eyal282 is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 08-27-2018 , 05:39   Re: StringToFloat gives 0.3499999
Reply With Quote #2

You can't exactly represent some numbers with a float. You can use the format specifier to set the number of digits to print (e.g. %.2f).
Fyren is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 08-27-2018 , 05:45   Re: StringToFloat gives 0.3499999
Reply With Quote #3

Quote:
Originally Posted by Fyren View Post
You can't exactly represent some numbers with a float. You can use the format specifier to set the number of digits to print (e.g. %.2f).
What if I have a string that is equal to 0.350000, how to reduce the zeros? As a decent scripter I'm amazed I encountered such weird issue, but don't hate the player, hate the game
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334
eyal282 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 08-27-2018 , 05:51   Re: StringToFloat gives 0.3499999
Reply With Quote #4

Loop characters from the back and put the null character where you encounter the first non-'0' character.
__________________
klippy is offline
Silvers
SourceMod Plugin Approver
Join Date: Aug 2010
Location: SpaceX
Old 08-27-2018 , 07:29   Re: StringToFloat gives 0.3499999
Reply With Quote #5

Quote:
Originally Posted by eyal282 View Post
how to reduce the zeros?
Fyren literally said in the message you quoted.
__________________
Silvers is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 08-27-2018 , 08:58   Re: StringToFloat gives 0.3499999
Reply With Quote #6

Quote:
Originally Posted by Silvers View Post
Fyren literally said in the message you quoted.
Format(buffer, length, "%.2f", StringToFloat(buffer));

Like this? Because I think it'll give me 0.3499999
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334
eyal282 is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 08-27-2018 , 12:58   Re: StringToFloat gives 0.3499999
Reply With Quote #7

Quote:
Originally Posted by eyal282 View Post
Format(buffer, length, "%.2f", StringToFloat(buffer));

Like this? Because I think it'll give me 0.3499999
Well, it wont.
__________________
asherkin is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 08-27-2018 , 19:08   Re: StringToFloat gives 0.3499999
Reply With Quote #8

Quote:
Originally Posted by eyal282 View Post
Format(buffer, length, "%.2f", StringToFloat(buffer));

Like this? Because I think it'll give me 0.3499999
Do... do you not see the .2 between the % and the f? That is literally telling Sourcemod to only print two digits after the decimal point.
__________________

Last edited by ddhoward; 08-27-2018 at 19:08.
ddhoward is offline
Skyy
AlliedModders Donor
Join Date: Jan 2010
Location: Toronto, Canada
Old 08-28-2018 , 06:28   Re: StringToFloat gives 0.3499999
Reply With Quote #9

Like Asherkin and others have said, for example I use %3.3f very often when I am using floats with talents to display the information to players, otherwise there would be many points beyond the 3 that i request in code. You would then see 0.349
Skyy 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 18:00.


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