0

I have a code like this in my view:

<div id="price"> 2000000 </div>

I want a Javascript code that add "," for each three digits from right and in my code it's should be: 2,000,000

I'm beginner in javascript and i seen a few topics about this code and none of them was complete (with html codes) , please explain how i use this javascript code in my html code How can I do this?

2

2 Answers 2

1

use n.toLocaleString(). it will convert number to comma separated number.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm beginner in java script i'm not sure to how use this javascripts code in my div id . please explain complete code . thanks
0

You can use toLocaleString() method on a number in javascript, to get your required result.

var priceDiv = document.getElementById("price")
var price = +priceDiv.innerHTML;
priceDiv.innerHTML = price.toLocaleString()
<div id="price"> 2000000 </div>

More on toLocaleString : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.