Raised This Month: $ Target: $400
 0% 

How do you display 0.01 through Format?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
dilalmon
AlliedModders Donor
Join Date: Apr 2013
Old 11-26-2013 , 03:36   How do you display 0.01 through Format?
Reply With Quote #1

I have a value that's 0.01, but I'm having difficulties trying to display it.

Code:
    Format(buffer, sizeof(buffer), "dMod: %.2f", dMod );
dMod is a Float variable and usually it's 0.01 for new clients.
The problem is because floats are approximations the true value will be 0.00999..., and it will cut to the second point of the decimal which will result "0.00".

Is there any way to solve this?
dilalmon is offline
Marcus_Brown001
AlliedModders Donor
Join Date: Nov 2012
Location: Illinois, United States
Old 11-26-2013 , 04:09   Re: How do you display 0.01 through Format?
Reply With Quote #2

You could use the native RoundToCeil on that variable.
Marcus_Brown001 is offline
dilalmon
AlliedModders Donor
Join Date: Apr 2013
Old 11-26-2013 , 04:21   Re: How do you display 0.01 through Format?
Reply With Quote #3

I don't understand. How would that return 0.01 if it would return an integer value?
dilalmon is offline
Bimbo1
Senior Member
Join Date: Jan 2010
Location: brazil
Old 11-26-2013 , 06:52   Re: How do you display 0.01 through Format?
Reply With Quote #4

make the float a string and interprete the values yourself.
__________________
sie sind das essen und wir sind die jäger!
Bimbo1 is offline
friagram
Veteran Member
Join Date: Sep 2012
Location: Silicon Valley
Old 11-26-2013 , 08:49   Re: How do you display 0.01 through Format?
Reply With Quote #5

Try roundtoceil(val * precision)/precision, with precision being the number of decimal places you need.. 10 100 1000 etc. either do some if statements, or if you have no idea where it will be, throw it in a string and di s where it is? Seems hacky.
__________________
Profile - Plugins
Add me on steam if you are seeking sp/map/model commissions.
friagram is offline
Marcus_Brown001
AlliedModders Donor
Join Date: Nov 2012
Location: Illinois, United States
Old 11-26-2013 , 19:34   Re: How do you display 0.01 through Format?
Reply With Quote #6

Quote:
Originally Posted by friagram View Post
Try roundtoceil(val * precision)/precision, with precision being the number of decimal places you need.. 10 100 1000 etc.
That is what I meant Sorry that I didn't elaborate.
Marcus_Brown001 is offline
Bimbo1
Senior Member
Join Date: Jan 2010
Location: brazil
Old 11-26-2013 , 19:39   Re: How do you display 0.01 through Format?
Reply With Quote #7

i bet it won't change a thing
__________________
sie sind das essen und wir sind die jäger!
Bimbo1 is offline
necavi
Veteran Member
Join Date: Sep 2010
Old 11-26-2013 , 20:02   Re: How do you display 0.01 through Format?
Reply With Quote #8

format("0.%02d", RoundToCeil(val * 100)) should work for values below 1.0.
necavi is offline
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 11-27-2013 , 14:20   Re: How do you display 0.01 through Format?
Reply With Quote #9

Quote:
Originally Posted by necavi View Post
format("0.%02d", RoundToCeil(val * 100)) should work for values below 1.0.
Only between 1.0 and 0.1, inclusive.

To avoid this whole issue, I just tend to store the number as a string straight away, e.g. from a ConVar or from a command parameter.
__________________

Last edited by 11530; 11-27-2013 at 14:22.
11530 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 10:58.


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