1var subTotal="12.1345";// can also be int, float, string
2var subTotalFormatted=parseFloat(subTotal).toFixed(2); //"12.13"
3
1var subTotal="12.1345";// can also be int, float, string
2var subTotalFormatted=parseFloat(subTotal).toFixed(2); //"12.13"
3
4//#2
5function round(value, precision) {
6 var multiplier = Math.pow(10, precision || 0);
7 return Math.round(value * multiplier) / multiplier;
8}
9
10//#3
11var number = 12.3456789;
12var rounded = Math.round( number * 10 ) / 10;
13// rounded is 12.3
14
15//#4
16var numb = 123.23454;
17numb = numb.toFixed(2);
18
19//#5
20var num = 5.56789;
21var n = num.toFixed(2);
22
23//#6
24var num = 2;
25var roundedString = num.toFixed(2);// 2.00
26
27//#7
28let num = 12.5452411;
29num = num.toFixed(3); // 12.545
30
31//#8
32function round(num, places) {
33 num = parseFloat(num);
34 places = (places ? parseInt(places, 10) : 0)
35 if (places > 0) {
36 let length = places;
37 places = "1";
38 for (let i = 0; i < length; i++) {
39 places += "0";
40 places = parseInt(places, 10);
41 }
42 } else {
43 places = 1;
44 }
45 return Math.round((num + Number.EPSILON) * (1 * places)) / (1 * places)
46}
47
48round(1.005, 2); // 1.01
49round(1.005, "1"); // 1
50round("1.23", 1); // 1.2
51round("1.436", "2"); // 1.44
52
53//#9
54myNumber.toFixed(7);
55
56//#10 PHP
57round(12345.6789, 2) // 12345.68
58round(12345.6789, 1) // 12345.7
59
60//#11
61Math.round((num + Number.EPSILON) * 100) / 100
62
63//#12
64Math.round(3.14159) // 3
65Math.round(3.5) // 4
66Math.floor(3.8) // 3
67Math.ceil(3.2) // 4
1function round(num, places) {
2 num = parseFloat(num);
3 places = (places ? parseInt(places, 10) : 0)
4 if (places > 0) {
5 let length = places;
6 places = "1";
7 for (let i = 0; i < length; i++) {
8 places += "0";
9 places = parseInt(places, 10);
10 }
11 } else {
12 places = 1;
13 }
14 return Math.round((num + Number.EPSILON) * (1 * places)) / (1 * places)
15}
16
17round(1.005, 2); // 1.01
18round(1.005, "1"); // 1
19round("1.23", 1); // 1.2
20round("1.436", "2"); // 1.44