parseNumber: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "<!-- Note Section [A-Z]+ --> " to "")
(Added more details about parsing hex numbers)
 
(30 intermediate revisions by one other user not shown)
Line 1: Line 1:
{{RV|type=command
{{RV|type=command


| arma1
|game1= arma1
|version1= 1.00


|1.00
|game2= arma2
|version2= 1.00


|gr1= Strings
|game3= arma2oa
|version3= 1.50


| An SQF equivalent of C++ [http://www.cplusplus.com/reference/cstdlib/atof/ atof] function. Parses the string, interpreting its content as a floating point number.<br><br>
|game4= tkoh
The command first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes as many characters as possible that are valid following a syntax resembling that of [[Floating_Point_Number#Floating_Point_Literals|floating point literals]], and interprets them as a numerical value. The rest of the string after the last valid character is ignored and has no effect on the behavior of this command.<br><br>
|version4= 1.00
If the first sequence of non-whitespace characters in string does not form a valid floating-point number, or if no such sequence exists because either string is empty or contains only whitespace characters, no conversion is performed and the command returns 0.


| [[parseNumber]] string
|game5= arma3
|version5= 0.50


|p1= string: [[String]]
|gr1= Strings
 
| [[Number]]


|s2= [[parseNumber]] boolean {{Since|arma3|1.49.131731|y}}
|descr= Parses the string, interpreting its content as a floating point number.
{{Feature|important|This command does '''not''' work with strings representing hexadecimal numbers (e.g {{hl|"0xCF"}}).
<sqf>
parseNumber "512 + 0x2F"; // 512, WRONG!
"512 + 0x2F" call BIS_fnc_parseNumber // 559, CORRECT!
</sqf>}}
{{Feature|informative|Starting whitespaces are discarded, then as many valid characters are used to be interpreted as a number until an invalid character is found then the rest of the string is ignored.}}


|p21= boolean: [[Boolean]]
|s1= [[parseNumber]] value


|r2= [[Number]] - 0 if [[false]], 1 if [[true]]
|p1= value: [[String]] or {{GVI|arma3|1.50|size= 0.75}} [[Boolean]]
 
|x1= <code>_number = [[parseNumber]] "0.125"; {{cc|0.125}}</code>


|x2= <code>_number = [[parseNumber]] "0050"; {{cc|50}}</code>
|r1= [[Number]] - the parsed number, {{hl|0}} if no valid number was found; in the case of a passed [[Boolean]], 0 if [[false]], 1 if [[true]]


|x3= <code>_number = [[parseNumber]] "2 abc"; {{cc|2}}</code>
|x1= <sqf>
_number = parseNumber "0.125"; // 0.125
_number = parseNumber "0050"; // 50
_number = parseNumber " 42 yes"; // 42
_number = parseNumber "3 or 4"; // 3
_number = parseNumber "either 3 or 4"; // 0
_number = parseNumber "0xCF"; // 0
</sqf>


|x4= <code>_number = [[parseNumber]] [[true]]; {{cc|1}}</code>
|x2= since {{arma3}} v1.50:
<sqf>
_result = parseNumber false; // 0
_result = parseNumber true; // 1
_zeroOneStatus = parseNumber alive player; // 0 if the player is dead, 1 if alive
</sqf>


|seealso= [[BIS_fnc_parseNumber]]
|seealso= [[BIS_fnc_parseNumber]] {{Link|http://www.cplusplus.com/reference/cstdlib/atof/|atof (C++)}} [[Floating Point Number#Floating Point Literals|Floating point literals]]
}}
}}
<dl class="command_description">
<dt><dt>
<dd class="notedate">Posted on Aug 28, 2014</dd>
<dt class="note">[[User:ffur2007slx2_5|ffur2007slx2_5]]</dt>
<dd class="note"> (A3 1.26)It only detects the first occurrence of a number in the string, return 0 by default.
<code>[[parseNumber]] "2s4f"; // 2
[[parseNumber]] "s2f4"; // 0</code>
parse config or code, currently [[BIS_fnc_parseNumber]], occurrence is only limited with number, return -1 by default.
<code>{s3s4f} [[call]] [[BIS_fnc_parseNumber]]; // -1</code>
</dl>
[[Category:Scripting Commands|{{uc:{{PAGENAME}}}}]]
{{GameCategory|arma1|Scripting Commands}}
{{GameCategory|arma2|Scripting Commands}}
{{GameCategory|arma3|Scripting Commands}}
{{GameCategory|tkoh|Scripting Commands}}
<dl class="command_description">
<dt></dt>
<dd class="notedate">Posted on April 5, 2021 - 14:56 (UTC)</dd>
<dt class="note">[[User:R3vo|R3vo]]</dt>
<dd class="note">{{#if:
This command '''does not''' work with strings representing hexadecimal numbers: <code>[[parseNumber]] "0xCF"; //Returns 0</code>
|
This command '''does not''' work with strings representing hexadecimal numbers: <code>[[parseNumber]] "0xCF"; //Returns 0</code>
|-no text provided-}}</dd>
</dl>

Latest revision as of 11:39, 10 May 2024

Hover & click on the images for description

Description

Description:
Parses the string, interpreting its content as a floating point number.
This command does not work with strings representing hexadecimal numbers (e.g "0xCF").
parseNumber "512 + 0x2F"; // 512, WRONG! "512 + 0x2F" call BIS_fnc_parseNumber // 559, CORRECT!
Starting whitespaces are discarded, then as many valid characters are used to be interpreted as a number until an invalid character is found then the rest of the string is ignored.
Groups:
Strings

Syntax

Syntax:
parseNumber value
Parameters:
value: String or Arma 3 logo black.png1.50 Boolean
Return Value:
Number - the parsed number, 0 if no valid number was found; in the case of a passed Boolean, 0 if false, 1 if true

Examples

Example 1:
_number = parseNumber "0.125"; // 0.125 _number = parseNumber "0050"; // 50 _number = parseNumber " 42 yes"; // 42 _number = parseNumber "3 or 4"; // 3 _number = parseNumber "either 3 or 4"; // 0 _number = parseNumber "0xCF"; // 0
Example 2:
since Arma 3 v1.50:
_result = parseNumber false; // 0 _result = parseNumber true; // 1 _zeroOneStatus = parseNumber alive player; // 0 if the player is dead, 1 if alive

Additional Information

See also:
BIS_fnc_parseNumber atof (C++) Floating point literals

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note