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

Decimal Format Float to Comma-Separated String

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.


For more Practice: Solve these Related Problems:

  • Write a JavaScript program that converts a floating-point number to a string with comma separators for thousands.
  • Write a JavaScript function that formats a decimal number into a localized string with appropriate commas.
  • Write a JavaScript program that takes a float and returns its string representation in decimal format with grouping commas.

Go to:


PREV : Reduce Array-Like to Hash Map.
NEXT : Format Number as Currency.

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.