toFixed: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - " <!-- (DIS)?CONTINUE Notes -->" to "")
m (Text replacement - "\[https?\:\/\/en\.wikipedia\.org\/wiki\/([^ ]+) (.+)\]" to "{{Wikipedia|$1|$2}}")
Line 10: Line 10:


| Converts a number into a string, keeping the specified number of decimals. If the desired number of decimals is higher than the actual number, nulls are added to create the desired decimal length. This command is almost identical in behaviour to JavaScript [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed toFixed()]<br><br>
| Converts a number into a string, keeping the specified number of decimals. If the desired number of decimals is higher than the actual number, nulls are added to create the desired decimal length. This command is almost identical in behaviour to JavaScript [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed toFixed()]<br><br>
'''NOTE:''' Converted number is never presented in scientific notation unlike with other number to string commands. Also Arma's default string conversion limits numbers to 6 [https://en.wikipedia.org/wiki/Significant_figures significant figures], whereas with this command it is possible to preserve some extra precision.
'''NOTE:''' Converted number is never presented in scientific notation unlike with other number to string commands. Also Arma's default string conversion limits numbers to 6 {{Wikipedia|Significant_figures|significant figures}}, whereas with this command it is possible to preserve some extra precision.
<code>[[str]] ([[pi]]/100000); //"3.14159e-005" - scientific notation
<code>[[str]] ([[pi]]/100000); //"3.14159e-005" - scientific notation
([[pi]]/100000) [[toFixed]] 10; //"0.0000314159" - no scientific notation
([[pi]]/100000) [[toFixed]] 10; //"0.0000314159" - no scientific notation

Revision as of 16:57, 30 May 2021

Hover & click on the images for description

Description

Description:
Description needed
Groups:
MathStrings

Syntax

Syntax:
Syntax needed
Parameters:
number: Number - number to convert
decimals: Number - number of decimals to display (range 0-20)
Return Value:
Return value needed

Alternative Syntax

Syntax:
toFixed decimals Template:Since
Parameters:
decimals: Number - number of decimals to display (range 0-20). -1 to reset to default number of decimals
Return Value:
Nothing

Examples

Example 1:
123 toFixed 2; // "123.00"
Example 2:
2.34 toFixed 1; // "2.3" 2.35 toFixed 1; // "2.4"
Example 3:
Convert position to string preserving position precision: fn_posToString = { format [ "[%1,%2,%3]", _this select 0 toFixed 8, _this select 1 toFixed 8, _this select 2 toFixed 8 ] }; str getPos player; // "[3231.05,171.802,0.00143862]" getPos player call fn_posToString; // "[3231.04882813,171.80192566,0.00143862]"
Example 4:
Same as Example 3 only using new alternative syntax:str getPos player; // "[3231.05,171.802,0.00143862]" toFixed 8; str getPos player; // "[3231.04882813,171.80192566,0.00143862]"

Additional Information

See also:
toStringtoArraytoLowertoUppertoLowerANSItoUpperANSI

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
Posted on September 2, 2016 - 21:56 (UTC)
R3vo
parseNumber (3.56346 toFixed 4); //0.0026 ms (10000 cycles)

[3.5634,4] call BIS_fnc_cutDecimals; //0.0111 ms (10000 cycles)