UGX-Mods Login

or login with an authentication provider below
Sign In with Google
Sign In with Twitter
Sign In with Discord
Sign In with Steam
Sign In with Facebook
Sign In with Twitch

[Function] Converting a string to a floating point number

broken avatar :(
Created 9 years ago
by JR-Imagine
0 Members and 1 Guest are viewing this topic.
2,894 views
broken avatar :(
×
broken avatar :(
Location: be
Date Registered: 17 August 2013
Last active: 3 years ago
Posts
369
Respect
Forum Rank
Perk Hacker
Primary Group
Community Scripter
My Groups
More
My Contact & Social Links
More
Personal Quote
Web & Software Developer and Designer
Signature
"Deleted code is debugged code." - Jeff Sickel
"Mathematicians stand on each others' shoulders and computer scientists stand on each others' toes." - Richard Hamming
×
JR-Imagine's Groups
Community Scripter Has shown effort and knowledge in the area of scripting while being a part of the UGX-Mods community.
I got bored so decided to write some random functions and ended up writing something that might be somewhat useful to someone... It converts a string to a floating point number (or integer if it's not a decimal number). Have fun:
Code Snippet
Plaintext
toFloat( in ) {
// Don't accept arrays or undefined, return 0
if( isArray( in ) || !isDefined( in ) )
return 0;

// Return original argument, it's not a string so doesn't need any special type conversion algorithm
if( !isString( in ) )
return in;

// Split string into 2 seperate strings
if( isSubStr( in, "," ) ) // Would be great if people wouldn't use fucking commas for decimals where I live
num = strTok( in, "," );
else
num = strTok( in, "." );

// Don't need to execute extra logic if the number isn't a decimal and therefore wasn't split into a multi-element array
if( num.size <= 1 )
return int( num[0] );

pwr = 10;
// Multiply by 10 for each extra character
// Initialize i as 1, we don't need to multiply on the first index
for( i = 1; i < num[1].size; i++ )
pwr *= 10;

return int( num[0] ) + int( num[1] ) / pwr;
}

Example usage:
Code Snippet
Plaintext
number = toFloat( "666.66" ); // type = float, value = 666.66
broken avatar :(
×
broken avatar :(
Location: esMadrid
Date Registered: 27 March 2015
Last active: 3 years ago
Posts
371
Respect
Forum Rank
Perk Hacker
Primary Group
Member
My Contact & Social Links
More
Personal Quote
JIGGLYPUFF used SING! YOU fell asleep!
Signature
×
Soy-Yo's Groups
Soy-Yo's Contact & Social LinksSoy-yoElCerdoRey
Code Snippet
Plaintext
if( isSubStr( in, "," ) ) // Would be great if people wouldn't use fucking commas for decimals where I live
Haven't you think on people like me, who use a  '  instead of a point or a comma? :alone:

Interesting fact: in Spain we use the point where you use the comma and the comma where you use the point. :P Don't know if other countries do the same.

Anyway, good function. I thought it was done automatically by the game (it should >:().
broken avatar :(
×
broken avatar :(
Location: gbMilton Keynes
Date Registered: 17 January 2014
Last active: 4 years ago
Posts
6,877
Respect
1,004Add +1
Forum Rank
Immortal
Primary Group
Community Scripter
My Groups
More
My Contact & Social Links
More
Signature
If you want scripts / features made for you, then contact me by PM or email / skype etc
it will cost you tho so if you have no intention of reciprocating don't even waste my time ;)
×
Harry Bo21's Groups
Donator ♥ Benevolent Soul who has our eternal gratitude and exclusive access to betas and the donator section of the forum.
Community Scripter Has shown effort and knowledge in the area of scripting while being a part of the UGX-Mods community.
Harry Bo21's Contact & Social Links[email protected]HarryBo21HarryBo000
Haven't you think on people like me, who use a  '  instead of a point or a comma? :alone:

Interesting fact: in Spain we use the point where you use the comma and the comma where you use the point. :P Don't know if other countries do the same.

Anyway, good function. I thought it was done automatically by the game (it should >:().
funny you say that, the BO2 decompiled code often substitutes "." for ",", meaning i have to track all instances down. In some args, can look too complicated lol

wait 0,1; is the most common i seem to find

usually , is to define a tenth or a thousand, or for grammar

10,000 = ten thousand, the , is just to make it "clearer"



i see its actually more countries use the comma version than our point version too ? lol

Quote
Countries using Arabic numerals with decimal point[edit]
Countries where a dot "." is used as decimal mark include:

Australia
Bangladesh
Botswana
British West Indies
Brunei
Canada (when using English)
China, People's Republic of
Dominican Republic
Egypt
Ghana
Guatemala
Honduras
Hong Kong
India
Ireland
Israel
Japan
Jordan
Kenya
Korea (both North and South)
Lebanon
Luxembourg (uses both marks officially)
Macau (in Chinese and English text)
Malaysia
Malta
Mexico
Mongolia
Nepal
New Zealand
Nicaragua
Nigeria
Pakistan
Palestine
Panama
Philippines
Singapore
Sri Lanka
Switzerland
Taiwan
Tanzania
Thailand
Uganda
United Kingdom
United States (including insular areas)
Zimbabwe

Countries using Arabic numerals with decimal comma[edit]
Countries where a comma "," is used as decimal mark include:

Albania
Algeria
Andorra
Angola
Argentina
Armenia
Austria
Azerbaijan
Belarus
Belgium
Bolivia
Bosnia and Herzegovina
Brazil
Bulgaria
Cameroon
Canada (when using French)
Chile
Colombia
Costa Rica
Croatia (comma used officially, but both forms are in use)
Cuba
Cyprus
Czech Republic
Denmark
East Timor
Ecuador
Estonia
Faroes
Finland
France
Germany
Georgia
Greece
Greenland
Hungary
Iceland
Indonesia
Italy
Kazakhstan
Kosovo
Kyrgyzstan
Latvia
Lebanon
Lithuania
Luxembourg (uses both marks officially)
Macau (in Portuguese text)
Macedonia
Moldova
Mongolia
Morocco
Mozambique
Namibia
The Netherlands
Norway
Paraguay
Peru
Poland
Portugal
Romania
Russia
Serbia
Slovakia
Slovenia
South Africa
Spain
Switzerland
Sweden
Tunisia
Turkey
Ukraine
Uruguay
Uzbekistan
Venezuela
Vietnam
Last Edit: October 10, 2015, 11:12:47 pm by Harry Bo21
broken avatar :(
×
broken avatar :(
Location: esMadrid
Date Registered: 27 March 2015
Last active: 3 years ago
Posts
371
Respect
Forum Rank
Perk Hacker
Primary Group
Member
My Contact & Social Links
More
Personal Quote
JIGGLYPUFF used SING! YOU fell asleep!
×
Soy-Yo's Groups
Soy-Yo's Contact & Social LinksSoy-yoElCerdoRey
funny you say that, the BO2 decompiled code often substitutes "." for ",", meaning i have to track all instances down. In some args, can look too complicated lol

wait 0,1; is the most common i seem to find

usually , is to define a tenth or a thousand, or for grammar

10,000 = ten thousand, the , is just to make it "clearer"



i see its actually more countries use the comma version than our point version too ? lol
Again no one thinks on people who use this '. :alone: I think this time it has been Wikipedia, hasn't it?
I didn't know we were more lol. I thought more countries used the dot for decimals.

And I use 10 000 for ten thousand (with a little space to make it clearer). And 0'1 for decimals.
broken avatar :(
×
broken avatar :(
Location: gbMilton Keynes
Date Registered: 17 January 2014
Last active: 4 years ago
Posts
6,877
Respect
1,004Add +1
Forum Rank
Immortal
Primary Group
Community Scripter
My Groups
More
My Contact & Social Links
More
×
Harry Bo21's Groups
Donator ♥ Benevolent Soul who has our eternal gratitude and exclusive access to betas and the donator section of the forum.
Community Scripter Has shown effort and knowledge in the area of scripting while being a part of the UGX-Mods community.
Harry Bo21's Contact & Social Links[email protected]HarryBo21HarryBo000
Again no one thinks on people who use this '. :alone: I think this time it has been Wikipedia, hasn't it?
I didn't know we were more lol. I thought more countries used the dot for decimals.

And I use 10 000 for ten thousand (with a little space to make it clearer). And 0'1 for decimals.
yes it was from wiki, and if you look he actually allowed for both

Code Snippet
Plaintext
// Split string into 2 seperate strings
if( isSubStr( in, "," ) ) // Would be great if people wouldn't use fucking commas for decimals where I live
num = strTok( in, "," );
else
num = strTok( in, "." );
broken avatar :(
×
broken avatar :(
The King of Zombies
Location: usLouisiana
Date Registered: 24 June 2013
Last active: 4 years ago
Posts
2,148
Respect
Forum Rank
King of the Zombies
Primary Group
Donator ♥
My Groups
More
My Contact & Social Links
More
Signature
Donate to me if you enjoy my work. https://www.paypal.me/thezombiekilla6
×
Dust's Groups
Donator ♥ Benevolent Soul who has our eternal gratitude and exclusive access to betas and the donator section of the forum.
Dust's Contact & Social LinksMrZ0mbiesFanaticdust103194MrZ0mbiesFanatic
That explains why when I talk to other people from other countries they use "," instead of "." I always thought it was a typo  :poker:
broken avatar :(
×
broken avatar :(
Location: gbMilton Keynes
Date Registered: 17 January 2014
Last active: 4 years ago
Posts
6,877
Respect
1,004Add +1
Forum Rank
Immortal
Primary Group
Community Scripter
My Groups
More
My Contact & Social Links
More
×
Harry Bo21's Groups
Donator ♥ Benevolent Soul who has our eternal gratitude and exclusive access to betas and the donator section of the forum.
Community Scripter Has shown effort and knowledge in the area of scripting while being a part of the UGX-Mods community.
Harry Bo21's Contact & Social Links[email protected]HarryBo21HarryBo000
That explains why when I talk to other people from other countries they use "," instead of "." I always thought it was a typo  :poker:
lol me too, and i thought the bo2 gscs were some sort of problem during the decryption process. When Im gonna assume now that they were decompiled from a Spanish version or something maybe
Last Edit: October 11, 2015, 12:17:54 am by Harry Bo21
broken avatar :(
×
broken avatar :(
Location: us
Date Registered: 30 December 2012
Last active: 9 months ago
Posts
577
Respect
Forum Rank
Zombie Enslaver
Primary Group
Community Scripter
My Groups
More
My Contact & Social Links
More
Signature
My preferred name is "xSanchez78".
Check me out here: www.steamcommunity.com/id/xSanchez78
×
alaurenc9's Groups
Donator ♥ Benevolent Soul who has our eternal gratitude and exclusive access to betas and the donator section of the forum.
Community Scripter Has shown effort and knowledge in the area of scripting while being a part of the UGX-Mods community.
alaurenc9's Contact & Social LinksxSanchez78xSanchez78xSanchez78xSanchez78xSanchez78xSanchez78
this function already exists in _zombiemode_utility btw, it is called string_to_float(). the one in world at war is a bit buggy however, but i found a perfectly working one in the black ops 1 version of _zombiemode_utility

 
Loading ...