Raised This Month: $ Target: $400
 0% 

Cut decimal places (float to string)


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author Message
Timiditas
Senior Member
Join Date: Apr 2009
Old 10-17-2009 , 10:42   Cut decimal places (float to string)
Reply With Quote #1

Forum server hiccuped while posting this and now there's a corpse thread in the list
http://forums.alliedmods.net/showthread.php?t=106631

Strip a float to any number of decimal places, up to 1.
(There's no sense it stripping ALL places with this function, since you just could use RoundToFloor and IntToString for this)

Usage:
PHP Code:
new String:strPI[64];
FloatToCutString(3.141592653589793strPIsizeof(strPI), 2)
PrintToChatAll("PI = %s"strPI);

ResultPI 3,14 
PHP Code:
bool:FloatToCutString(Float:valueString:Target[], TargetSizeDecPlaces)
{
    if(
DecPlaces 1)
        return 
false;
    new 
Float:fBuffer valueString:Buffer[255], String:Buffer2[255];
    new 
Ganz RoundToFloor(fBuffer);    //strip integer from decimal places
    
fBuffer FloatFraction(fBuffer);    //strip decimal places from integer
    
FloatToString(fBufferBuffer, (3+DecPlaces)); //cut decimal places to desired (2 places = 5 characters (0,xx\n)
    
strcopy(Buffer2sizeof(Buffer2), Buffer[2]); //strip decimal places string from '0,'-prefix
    
Format(Buffersizeof(Buffer), "%i,%s",Ganz,Buffer2);
    
strcopy(TargetTargetSizeBuffer);
    return 
true;

__________________


Last edited by Timiditas; 10-17-2009 at 10:52.
Timiditas is offline
 


Thread Tools
Display Modes

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:46.


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