Raised This Month: $ Target: $400
 0% 

Cut decimal places (float to string)


Post New Thread Reply   
 
Thread Tools Display Modes
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
Theme97
Senior Member
Join Date: Mar 2009
Old 10-17-2009 , 14:52   Re: Cut decimal places (float to string)
Reply With Quote #2

Well, considering Format() already does this...

PHP Code:
stock FloatToCutString(Float:valueString:target[], targetSizeDecPlaces) {
    
decl String:fmt[8];
    
FormatEx(fmtsizeof(fmt), "%%.%df"DecPlaces); // e.g., 2 places = %.2f
    
FormatEx(targettargetSizefmtvalue);

To keep it as a float (though this may cause an overflow),

PHP Code:
stock Float:RoundFloatToPlaces(Float:valueDecPlaces) {
    if (!
DecPlaces) return float(RoundToNearest(value));
    new 
Float:mult Pow(10.0float(DecPlaces));
    return 
RoundToNearest(value mult) / mult;

__________________
I now have very little time to work on stuff and my main computer (which is the only one that can run TF2) is not cooperating with me, so don't expect many updates from me anytime soon.

[ALL] Karaoke
[TF2] Intel Timer | Crabmod | Randomizer | Stopwatch | Crits 4 All

Last edited by Theme97; 10-17-2009 at 15:09.
Theme97 is offline
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 10-18-2009 , 16:54   Re: Cut decimal places (float to string)
Reply With Quote #3

%.nf is all you need... forget all the unneeded code...
you should research better before you waste your own time
__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0
berni is offline
Timiditas
Senior Member
Join Date: Apr 2009
Old 10-18-2009 , 17:00   Re: Cut decimal places (float to string)
Reply With Quote #4

I don't consider that time as wasted, but you're right nonetheless.
__________________

Timiditas is offline
Reply


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