6 examples of 'javascript format float' in JavaScript

Every line of 'javascript format float' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
16export function floatFormat(n) {
17 const pows = {
18 1: { text: 'B', pow: 0 },
19 2: { text: 'B', pow: 0 },
20 3: { text: 'B', pow: 0 },
21 4: { text: 'Kb', pow: 3 },
22 5: { text: 'Kb', pow: 3 },
23 6: { text: 'Kb', pow: 3 },
24 7: { text: 'Mb', pow: 6 },
25 8: { text: 'Mb', pow: 6 },
26 9: { text: 'Mb', pow: 6 },
27 10: { text: 'Gb', pow: 9 },
28 11: { text: 'Gb', pow: 9 },
29 }
30
31 const len = String(n).length
32 const fmt = (n / Number(`1e${pows[len].pow}`)).toFixed(2)
33 const str = `${fmt} ${pows[len].text}`
34
35 return str
36}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

24export function floatToString(float) {
25 return float.toFixed(2).replace(/[0\.]+$/, '');
26}
9function float_precision(float_value, precision) {
10 float_value = parseFloat(float_value);
11 if (isNaN(float_value)) {
12 return parseFloat('0').toFixed(precision);
13 }
14 else {
15 var power = Math.pow(10, precision);
16 float_value = (Math.round(float_value * power) / power).toFixed(precision);
17 return float_value.toString();
18 }
19}
18function Float (value) {
19 return Number(value)
20}
115function formatDecimal(valueAsNumber) {
116 if (!hasDecimalPlaces(valueAsNumber)) {
117 return Math.round(valueAsNumber).toString();
118 }
119 let text = valueAsNumber.toFixed(1);
120 return text.endsWith('.0') ? Math.round(valueAsNumber).toString() : text;
121}
17public format(value: number): string {
18 return value.toFixed(this.digits_);
19}

Related snippets