w3resource

JavaScript: Convert a float-point arithmetic to the Decimal mark form and It will make a comma separated string from a number

JavaScript: Exercise-125 with Solution

Write a JavaScript program that converts float-point arithmetic to decimal form, and creates a comma separated string from a number.

  • Use Number.prototype.toLocaleString() to convert the number to decimal mark format.

Sample Solution:

JavaScript Code:

//#Source https://bit.ly/2neWfJ2 

// Define the 'toDecimalMark' function.
const toDecimalMark = num => num.toLocaleString('en-US');

// Test the 'toDecimalMark' function with sample inputs.
console.log(toDecimalMark(12305030388.9087)); // Output: "12,305,030,388.909"
console.log(toDecimalMark(123.2264)); // Output: "123.226"
console.log(toDecimalMark(-100.10)); // Output: "-100.1"

Output:

12,305,030,388.909
123.226
-100.1

Flowchart:

flowchart: Convert a float-point arithmetic to the Decimal mark form and It will make a comma separated string from a number

Live Demo:

See the Pen javascript-basic-exercise-125-1 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus

Previous: Write a JavaScript program to reduce a given Array-like into a value hash (keyed data store).
Next: Write a JavaScript program to create a specified currency formatting from a given number.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/javascript-exercises/fundamental/javascript-fundamental-exercise-125.php