{"name":"toFixed","operator":null,"title":"toFixed","type":"command","url":"https://community.bistudio.com/wiki/toFixed","revid":369349,"updated":"2024-09-03T18:43:03Z","groups":["Math","Strings"],"status":"ok","replaced_by":[],"games":[{"game":"arma3","name":"Arma 3","version":"1.66"}],"since":{"game":"arma3","name":"Arma 3","version":"1.66"},"arma3_since":"1.66","branch":null,"locality":{"argument":null,"effect":null,"server_exec":false},"exec":null,"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()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed)\n\nThe 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.\nTo reset output back to default at any time, use `toFixed -1` - see #Example 5.\n\n> [!NOTE]\n> 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](https://en.wikipedia.org/wiki/Significant_figures), whereas with this command it is possible to preserve some extra precision - see #Example 6.","syntaxes":[{"syntax":"number toFixed decimals","since":null,"effect":null,"exec":null,"params":[{"name":"number","type":"Number","optional":false,"default":null,"description":"number to convert","types":["Number"],"since":null,"text":"number: Number - number to convert"},{"name":"decimals","type":"Number","optional":false,"default":null,"description":"number of decimals to display (range 0-20)","types":["Number"],"since":null,"text":"decimals: Number - number of decimals to display (range 0-20)"}],"returns":{"type":"String","types":["String"],"description":null,"text":"String"},"form":"binary","operator":null,"left":{"text":"number","types":["Number"]},"right":{"text":"decimals","types":["Number"]}},{"syntax":"toFixed decimals","since":"arma3 1.72","effect":null,"exec":null,"params":[{"name":"decimals","type":"Number","optional":false,"default":null,"description":"number of decimals to display (range 0-20). -1 to reset to default number of decimals","types":["Number"],"since":null,"text":"decimals: Number - number of decimals to display (range 0-20). -1 to reset to default number of decimals"}],"returns":{"type":"Nothing","types":["Nothing"],"description":null,"text":"Nothing"},"form":"unary","operator":null,"left":null,"right":{"text":"decimals","types":["Number"]}}],"examples":[{"text":"```sqf\n123 toFixed 2; // \"123.00\"\n```","code":["123 toFixed 2; // \"123.00\""]},{"text":"```sqf\n2.34 toFixed 1; // \"2.3\"\n2.35 toFixed 1; // \"2.4\"\n```","code":["2.34 toFixed 1; // \"2.3\"\n2.35 toFixed 1; // \"2.4\""]},{"text":"Convert position to string preserving position precision:\n\n```sqf\nfn_posToString = \n{\n    format [\n        \"[%1,%2,%3]\", \n        _this select 0 toFixed 8, \n        _this select 1 toFixed 8, \n        _this select 2 toFixed 8\n    ]\n};\nstr getPos player;                    // \"[3231.05,171.802,0.00143862]\"\ngetPos player call fn_posToString;    // \"[3231.04882813,171.80192566,0.00143862]\"\n```","code":["fn_posToString = \n{\n\tformat [\n\t\t\"[%1,%2,%3]\", \n\t\t_this select 0 toFixed 8, \n\t\t_this select 1 toFixed 8, \n\t\t_this select 2 toFixed 8\n\t]\n};\nstr getPos player;\t\t\t\t\t// \"[3231.05,171.802,0.00143862]\"\ngetPos player call fn_posToString;\t// \"[3231.04882813,171.80192566,0.00143862]\""]},{"text":"Same as **Example 3** only using new alternative syntax:\n\n```sqf\nstr getPos player; // \"[3231.05,171.802,0.00143862]\"\ntoFixed 8;\nstr getPos player; // \"[3231.04882813,171.80192566,0.00143862]\"\n```","code":["str getPos player; // \"[3231.05,171.802,0.00143862]\"\ntoFixed 8;\nstr getPos player; // \"[3231.04882813,171.80192566,0.00143862]\""]},{"text":"```sqf\nsystemChat str position player;        // [11580.3,11797.7,0.00146675]\ncall\n{\n    toFixed 6;\n    systemChat str position player;    // [11580.341797,11797.737305,0.001467]\n};\nsystemChat str position player;        // [11580.341797,11797.737305,0.001467]\ntoFixed -1;\nsystemChat str position player;        // [11580.3,11797.7,0.00146675]\n```\n\nIdeal to be used when saving data to a database when more precise positioning is required.","code":["systemChat str position player;\t\t// [11580.3,11797.7,0.00146675]\ncall\n{\n\ttoFixed 6;\n\tsystemChat str position player;\t// [11580.341797,11797.737305,0.001467]\n};\nsystemChat str position player;\t\t// [11580.341797,11797.737305,0.001467]\ntoFixed -1;\nsystemChat str position player;\t\t// [11580.3,11797.7,0.00146675]"]},{"text":"```sqf\nstr (pi/100000);        // \"3.14159e-005\" - scientific notation\n(pi/100000) toFixed 10;    // \"0.0000314159\" - no scientific notation\nstr pi;                    // \"3.14159\" - 6 significant figures (default)\npi toFixed 6;            // \"3.141593\" - forced to 7 significant figures\npi toFixed 7;            // \"3.1415927\" - forced to 8 significant figures\n```","code":["str (pi/100000);\t\t// \"3.14159e-005\" - scientific notation\n(pi/100000) toFixed 10;\t// \"0.0000314159\" - no scientific notation\nstr pi;\t\t\t\t\t// \"3.14159\" - 6 significant figures (default)\npi toFixed 6;\t\t\t// \"3.141593\" - forced to 7 significant figures\npi toFixed 7;\t\t\t// \"3.1415927\" - forced to 8 significant figures"]}],"multiplayer":null,"problems":null,"aliases":[],"see_also":["toString","toArray","toLower","toUpper","toLowerANSI","toUpperANSI"],"notes":[{"user":"R3vo","date":"2016-09-02T21:56:00Z","text":"```sqf\nparseNumber (3.56346 toFixed 4);        // 0.0026 ms (10000 cycles)\n[3.5634,4] call BIS_fnc_cutDecimals;    // 0.0111 ms (10000 cycles)\n```"}],"categories":["Arma 3: New Scripting Commands","Arma 3: Scripting Commands","Command Group: Math","Command Group: Strings","Introduced with Arma 3 version 1.66","Scripting Commands"]}