Raised This Month: $32 Target: $400
 8% 

Solved Rounding to the nearest tenth


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
impossible_cc
Senior Member
Join Date: Sep 2018
Location: Ukraine
Old 12-15-2018 , 05:35   Rounding to the nearest tenth
Reply With Quote #1

PHP Code:
float value 0.9;

PrintToChat(client"value == %f"value);

float value2 0.975;

PrintToChat(client"value2 == %f"value2); 
Displayed 0.90000000 (or 0.89999999) and 0.975000000000000 (or 0.97499999).

You can get rid of this nulls if you know how many significant numbers after the decimal point.
PHP Code:
void PrintToChatFloatint clientfloat numberint signif)
{
    
int integ RoundToFloor(number);
    
number -= integ;
    
int fraction RoundFloat(number pow(10signif) );
    
PrintToChat(client"%i.%i"integfraction);

Or if you want to use it later, dont print it, just return with array.

The problem is sometimes you dont know how many significant numbers in float number.

Is there any other way, to display floats in the correct form in char?

Last edited by impossible_cc; 12-15-2018 at 18:09.
impossible_cc is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 12-15-2018 , 09:09   Re: Rounding to the nearest tenth
Reply With Quote #2

https://wiki.alliedmods.net/Format_C...ced_Formatting

%.0f
%.2f
__________________
Do not Private Message @me

Last edited by Bacardi; 12-15-2018 at 12:41.
Bacardi is offline
impossible_cc
Senior Member
Join Date: Sep 2018
Location: Ukraine
Old 12-15-2018 , 18:09   Re: Rounding to the nearest tenth
Reply With Quote #3

Thanks, didn't know about it.
impossible_cc is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 12-15-2018 , 18:28   Re: Rounding to the nearest tenth
Reply With Quote #4

...it not literally round to nearest, but you can set how many decimal it show :/
Bacardi 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 07:37.


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