toFixed: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
(example)
m (Text replacement - "\[ *(https?:\/\/[^ = ]+) +([^= ]+) *\]" to "{{Link|$1|$2}}")
 
(83 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Command|= Comments
{{RV|type=command
____________________________________________________________________________________________


| arma3 |= Game name
|game1= arma3
|version1= 1.66


|1.66|= Game version
|gr1= Math
____________________________________________________________________________________________


| 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>
|gr2= Strings
'''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.
<code>[[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</code>


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 <tt>toFixed -1</tt>. For example:
|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>
<code>[[systemChat]] [[str]] [[position]] [[player]];
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.
[[call]]
To reset output back to default at any time, use {{hl|toFixed -1}} - see {{Link|#Example 5}}.
{
 
[[toFixed]] 6;
{{Feature|informative|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 {{Link|https://en.wikipedia.org/wiki/Significant_figures|significant figures}}, whereas with this command it is possible to preserve some extra precision - see {{Link|#Example 6}}.}}
[[systemChat]] [[str]] [[position]] [[player]];
 
};
|s1= number [[toFixed]] decimals
[[systemChat]] [[str]] [[position]] [[player]];
 
[[toFixed]] -1;
|p1= number: [[Number]] - number to convert
[[systemChat]] [[str]] [[position]] [[player]];</code>
 
The result is:
|p2= decimals: [[Number]] - number of decimals to display (range 0-20)
* <tt>[11580.3,11797.7,0.00146675]</tt>
* <tt>[11580.341797,11797.737305,0.001467]</tt>
* <tt>[11580.341797,11797.737305,0.001467]</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.
|= Description
____________________________________________________________________________________________


| number '''toFixed''' decimals |= Syntax
|r1= [[String]]


|p1= number: [[Number]] - number to convert |= Parameter 1
|s2= [[toFixed]] decimals
|p2= decimals: [[Number]] - number of decimals to display (range 0-20) |= Parameter 2


| [[String]] |= Return value
|s2since= arma3 1.72


| s2= '''toFixed''' decimals &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(''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|= Parameter 2
|r2= [[Nothing]]


| r2= [[Nothing]] |= Return value
|x1= <sqf>123 toFixed 2; // "123.00"</sqf>
____________________________________________________________________________________________


|x1= <code>123 [[toFixed]] 2; //"123.00"</code>|= Example 1
|x2= <sqf>2.34 toFixed 1; // "2.3"
|x2= <code>2.34 [[toFixed]] 1; //"2.3"
2.35 toFixed 1; // "2.4"</sqf>
2.35 [[toFixed]] 1; //"2.4"</code>|= Example 2


|x3= Convert position to string preserving position precision: <code>fn_posToString =  
|x3= Convert position to string preserving position precision:
<sqf>fn_posToString =  
{
{
[[format]] [
format [
"[%1,%2,%3]",  
"[%1,%2,%3]",  
_this [[select]] 0 [[toFixed]] 8,  
_this select 0 toFixed 8,  
_this [[select]] 1 [[toFixed]] 8,  
_this select 1 toFixed 8,  
_this [[select]] 2 [[toFixed]] 8
_this select 2 toFixed 8
]
]
};
};
[[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>|= Example 3
getPos player call fn_posToString; // "[3231.04882813,171.80192566,0.00143862]"</sqf>
 
|x4= Same as '''Example 3''' only using new alternative syntax:
<sqf>str getPos player; // "[3231.05,171.802,0.00143862]"
toFixed 8;
str getPos player; // "[3231.04882813,171.80192566,0.00143862]"</sqf>


|x4= Same as Example 3 only using new alternative syntax:<code>[[str]] [[getPos]] [[player]]; // "[3231.05,171.802,0.00143862]"
|x5= <sqf>
[[toFixed]] 8;
systemChat str position player; // [11580.3,11797.7,0.00146675]
[[str]] [[getPos]] [[player]]; // "[3231.04882813,171.80192566,0.00143862]"</code>|= Example 4
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]
</sqf>
Ideal to be used when saving data to a database when more precise positioning is required.


| [[toString]], [[toArray]], [[toLower]], [[toUpper]] |= See also
|x6= <sqf>
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
</sqf>


|seealso= [[toString]] [[toArray]] [[toLower]] [[toUpper]] [[toLowerANSI]] [[toUpperANSI]]
}}
}}


<h3 style="display:none">Notes</h3>
{{Note
<dl class="command_description">
|user= R3vo
 
|timestamp= 20160902215600
</dl>
|text= <sqf>
 
parseNumber (3.56346 toFixed 4); // 0.0026 ms (10000 cycles)
<h3 style="display:none">Bottom Section</h3>
[3.5634,4] call BIS_fnc_cutDecimals; // 0.0111 ms (10000 cycles)
[[Category:Scripting Commands|{{uc:{{PAGENAME}}}}]]
</sqf>
[[Category:Scripting Commands Arma 3|{{uc:{{PAGENAME}}}}]]
}}
 
<!-- CONTINUE Notes -->
<dl class="command_description">
<dd class="notedate">Posted on September 2, 2016 - 21:56 (UTC)</dd>
<dt class="note">[[User:Revo|Revo]]</dt>
<dd class="note">
[[parseNumber]] (3.56346 '''toFixed''' 4); //0.0026 ms (10000 cycles)<br><br>
[3.5634,4] [[call]] [[BIS_fnc_cutDecimals]]; //0.0111 ms (10000 cycles)
</dd>
</dl>
<!-- DISCONTINUE Notes -->

Latest revision as of 16:11, 28 April 2023

Hover & click on the images for description

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.
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 - see Example 6.
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:
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]"
Example 5:
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]
Ideal to be used when saving data to a database when more precise positioning is required.
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
R3vo - c
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)