how to convert to money format in JS [duplicate] - javascript

This question already has answers here:
Javascript Thousand Separator / string format [duplicate]
(15 answers)
Closed 6 years ago.
i have a simple int like "77600" I want to convert it to "77 600" (basically i need to add a simple whitespace after thousands).
tmp_total = parseInt(tmp_total,10); //77600
tmp_total = ...here goes some magic...;
$('#chekout_total #total').text(tmp_total);

try this:
tmp_total = parseInt(tmp_total,10); //77600
tmp_total = tmp_total.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1 ');
$('#chekout_total #total').text(tmp_total);
Hope this will help you! :)

Related

Replace string which is between two characters [duplicate]

This question already has answers here:
The .replace() method does change the string in place [duplicate]
(3 answers)
Closed 3 years ago.
I am having the following string 4 (10.3%) and would like to receive 4.
I tried:
let s = "4 (10.3%)"
s.replace("/\s+\((.*?)\)", "");
console.log(s)
However, I still get the initial string back.
Any suggestion what I am doing wrong?
I appreciate your replies!
Use this instead of regex
let s = "4 (10.3%)"
console.log(s.substr(0, s.indexOf(' ')))
Using split
let s = "4 (10.3%)"
console.log(s.split(" ")[0])

How to replace white space with the equivalent special character in string? [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 4 years ago.
Say we have:
Saudi Arabia
If I do:
loc.replace(/ /g,''));
Becomes:
SaudiArabia
What if I want to replace the white space with &nbsp?
Tried:
loc.replace(/ /g,'&nbsp')
I am looking for
Saudi&nbspArabia
Did you mean this
var loc = "soudi arabia";
loc = loc.replace(/\s/g,' ');
console.log(loc)

parse string and store elements to an array [duplicate]

This question already has answers here:
How to use split?
(4 answers)
Closed 9 years ago.
I have a string likes below
a_b_c_d
I hope to decode it to an array t as
t[0]->a
t[1]->b
t[2]->c
t[3]->d
Just wonder if there ia function of javascript can parse the string directly.
var string = "a_b_c_d";
var t = string.split('_');
DEMO FIDDLE
Just split the string
var t = "a_b_c_d".split("_");

how to remove the following text from a string in javascript? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Replace all spaces in a string with '+'
how to remove ,#contacts.Assessor.firstname# #contacts.Assessor.lastname# and
,#contacts.Assessor - Secondary.firstname# #contacts.Assessor - Secondary.lastname# from a
string in javascript?
Thanks
Simply use .replace()
string = string
.replace(',#contacts.Assessor - Secondary.firstname# #contacts.Assessor - Secondary.lastname#', '')
.replace(',#contacts.Assessor.firstname# #contacts.Assessor.lastname#', '');
Also take a loot at this related question

how to split into character group? [duplicate]

This question already has answers here:
Split large string in n-size chunks in JavaScript
(23 answers)
Closed 9 years ago.
I want to split string
"abcdefgh"
to
"ab","cd","ef","gh"
using javascript split()
"abcdefgh".split(???)
Can you please help ?
Instead of split, try match:
var text = "abcdefgh";
print(text.match(/../g));
prints:
ab,cd,ef,gh
as you can see on Ideone.

Categories

Resources