toFixed: Difference between revisions
Jump to navigation
Jump to search
Killzone Kid (talk | contribs) (example) |
m (template:command argument fix) |
||
Line 31: | Line 31: | ||
* <tt>[11580.3,11797.7,0.00146675]</tt> | * <tt>[11580.3,11797.7,0.00146675]</tt> | ||
Ideal to be used when saving data to a database when more precise positioning is required. | Ideal to be used when saving data to a database when more precise positioning is required. | ||
|= | |DESCRIPTION= | ||
____________________________________________________________________________________________ | ____________________________________________________________________________________________ | ||
| number '''toFixed''' decimals |= | | number '''toFixed''' decimals |SYNTAX= | ||
|p1= number: [[Number]] - number to convert |= | |p1= number: [[Number]] - number to convert |PARAMETER1= | ||
|p2= decimals: [[Number]] - number of decimals to display (range 0-20) |= | |p2= decimals: [[Number]] - number of decimals to display (range 0-20) |PARAMETER2= | ||
| [[String]] |= | | [[String]] |RETURNVALUE= | ||
| s2= '''toFixed''' decimals (''since Arma 3 v1.71.141859'')|= | | s2= '''toFixed''' decimals (''since Arma 3 v1.71.141859'')|SYNTAX= | ||
|p21= decimals: [[Number]] - number of decimals to display (range 0-20). -1 to reset to default number of decimals|= | |p21= decimals: [[Number]] - number of decimals to display (range 0-20). -1 to reset to default number of decimals|PARAMETER2= | ||
| r2= [[Nothing]] |= | | r2= [[Nothing]] |RETURNVALUE= | ||
____________________________________________________________________________________________ | ____________________________________________________________________________________________ | ||
|x1= <code>123 [[toFixed]] 2; //"123.00"</code>|= | |x1= <code>123 [[toFixed]] 2; //"123.00"</code>|EXAMPLE1= | ||
|x2= <code>2.34 [[toFixed]] 1; //"2.3" | |x2= <code>2.34 [[toFixed]] 1; //"2.3" | ||
2.35 [[toFixed]] 1; //"2.4"</code>|= | 2.35 [[toFixed]] 1; //"2.4"</code>|EXAMPLE2= | ||
|x3= Convert position to string preserving position precision: <code>fn_posToString = | |x3= Convert position to string preserving position precision: <code>fn_posToString = | ||
Line 62: | Line 62: | ||
}; | }; | ||
[[str]] [[getPos]] [[player]]; // "[3231.05,171.802,0.00143862]" | [[str]] [[getPos]] [[player]]; // "[3231.05,171.802,0.00143862]" | ||
[[getPos]] [[player]] [[call]] fn_posToString; // "[3231.04882813,171.80192566,0.00143862]"</code>|= | [[getPos]] [[player]] [[call]] fn_posToString; // "[3231.04882813,171.80192566,0.00143862]"</code>|EXAMPLE3= | ||
|x4= Same as Example 3 only using new alternative syntax:<code>[[str]] [[getPos]] [[player]]; // "[3231.05,171.802,0.00143862]" | |x4= Same as Example 3 only using new alternative syntax:<code>[[str]] [[getPos]] [[player]]; // "[3231.05,171.802,0.00143862]" | ||
[[toFixed]] 8; | [[toFixed]] 8; | ||
[[str]] [[getPos]] [[player]]; // "[3231.04882813,171.80192566,0.00143862]"</code>|= | [[str]] [[getPos]] [[player]]; // "[3231.04882813,171.80192566,0.00143862]"</code>|EXAMPLE4= | ||
____________________________________________________________________________________________ | ____________________________________________________________________________________________ | ||
| [[toString]], [[toArray]], [[toLower]], [[toUpper]] |= | | [[toString]], [[toArray]], [[toLower]], [[toUpper]] |SEEALSO= | ||
}} | }} |
Revision as of 14:50, 7 April 2019
Description
- Description:
- 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 toFixed()
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 significant figures, whereas with this command it is possible to preserve some extra precision.str (pi/100000); //"3.14159e-005" - scientific notation (pi/100000) toFixed 10; //"0.0000314159" - no scientific notation str pi; //"3.14159" - 6 significant figures (default) pi toFixed 6; //"3.141593" - forced to 7 significant figures pi toFixed 7; //"3.1415927" - forced to 8 significant figures
Since Arma 3 v1.71.141859 an alternative syntax is added, which takes no number and returns Nothing. Instead it acts as keyword and switches engine Number to String global conversion into desired format, from the moment it is applied until the end of script. To reset output back to default at any time, use toFixed -1. For example:systemChat str position player; call { toFixed 6; systemChat str position player; }; systemChat str position player; toFixed -1; systemChat str position player;
The result is:- [11580.3,11797.7,0.00146675]
- [11580.341797,11797.737305,0.001467]
- [11580.341797,11797.737305,0.001467]
- [11580.3,11797.7,0.00146675]
- Groups:
- Uncategorised
Syntax
- Syntax:
- number toFixed decimals
- Parameters:
- number: Number - number to convert
- decimals: Number - number of decimals to display (range 0-20)
- Return Value:
- String
Alternative Syntax
- Syntax:
- toFixed decimals (since Arma 3 v1.71.141859)
- 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
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
Notes
Bottom Section
- Posted on September 2, 2016 - 21:56 (UTC)
- Revo
-
parseNumber (3.56346 toFixed 4); //0.0026 ms (10000 cycles)
[3.5634,4] call BIS_fnc_cutDecimals; //0.0111 ms (10000 cycles)