toFixed: Difference between revisions
Jump to navigation
Jump to search
Lou Montana (talk | contribs) m (Text replacement - "pedia\.org\/([^w])" to "pedia.org/wiki/$1") |
Lou Montana (talk | contribs) m (Text replacement - "<sqf>([^↵][^<]*↵[^<]*)<\/sqf>" to "<sqf> $1 </sqf>") |
||
(One intermediate revision by the same user not shown) | |||
Line 8: | Line 8: | ||
|gr2= Strings | |gr2= Strings | ||
|descr= 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 | |descr= 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 {{Link|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed|toFixed()}}<br><br> | ||
The alternative syntax acts as keyword and switches engine's [[Number]] to [[String]] global conversion into desired format, from the moment it is applied until the end of script. | The alternative syntax acts as keyword and switches engine's [[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 {{hl|toFixed -1}} - see {{Link|#Example 5}}. | To reset output back to default at any time, use {{hl|toFixed -1}} - see {{Link|#Example 5}}. | ||
Line 32: | Line 32: | ||
|x1= <sqf>123 toFixed 2; // "123.00"</sqf> | |x1= <sqf>123 toFixed 2; // "123.00"</sqf> | ||
|x2= <sqf>2.34 toFixed 1; // "2.3" | |x2= <sqf> | ||
2.35 toFixed 1; // "2.4"</sqf> | 2.34 toFixed 1; // "2.3" | ||
2.35 toFixed 1; // "2.4" | |||
</sqf> | |||
|x3= Convert position to string preserving position precision: | |x3= Convert position to string preserving position precision: | ||
<sqf>fn_posToString = | <sqf> | ||
fn_posToString = | |||
{ | { | ||
format [ | format [ | ||
Line 46: | Line 49: | ||
}; | }; | ||
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]"</sqf> | getPos player call fn_posToString; // "[3231.04882813,171.80192566,0.00143862]" | ||
</sqf> | |||
|x4= Same as '''Example 3''' only using new alternative syntax: | |x4= Same as '''Example 3''' only using new alternative syntax: | ||
<sqf>str getPos player; // "[3231.05,171.802,0.00143862]" | <sqf> | ||
str getPos player; // "[3231.05,171.802,0.00143862]" | |||
toFixed 8; | toFixed 8; | ||
str getPos player; // "[3231.04882813,171.80192566,0.00143862]"</sqf> | str getPos player; // "[3231.04882813,171.80192566,0.00143862]" | ||
</sqf> | |||
|x5= <sqf> | |x5= <sqf> |
Latest revision as of 19:43, 3 September 2024
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()
The alternative syntax acts as keyword and switches engine's 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 - see Example 5. - Groups:
- MathStrings
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
- 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:
- Example 4:
- Same as Example 3 only using new alternative syntax:
- Example 5:
- Ideal to be used when saving data to a database when more precise positioning is required.systemChat str position player; // [11580.3,11797.7,0.00146675] call { toFixed 6; systemChat str position player; // [11580.341797,11797.737305,0.001467] }; systemChat str position player; // [11580.341797,11797.737305,0.001467] toFixed -1; systemChat str position player; // [11580.3,11797.7,0.00146675]
- Example 6:
- 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
Additional Information
- See also:
- toString toArray toLower toUpper toLowerANSI toUpperANSI
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 Sep 02, 2016 - 21:56 (UTC)
-
parseNumber (3.56346 toFixed 4); // 0.0026 ms (10000 cycles) [3.5634,4] call BIS_fnc_cutDecimals; // 0.0111 ms (10000 cycles)