parseFloat to display with comma [duplicate] - javascript

This question already has answers here:
How to format a number with commas as thousands separators?
(50 answers)
Closed 3 years ago.
Is there a way to use the below JavaScript code to use comma's? For example the var num = 1924.00 Is there way to get it to display as 1,924.00?
var num = parseFloat(totalAmount).toFixed(2);

You could use a specific regex like this one in the function below:
function format_currency( value ) {
return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
If you want to use it in conjuction with the .toFixed(), just do like this:
format_currency( 1245.3.toFixed(2) );
Hope it helps.

Related

How i add two variables [duplicate]

This question already has answers here:
How to add two strings as if they were numbers? [duplicate]
(20 answers)
Closed 3 years ago.
how to add two variables, I'm doing it like this and they are concatenating
id('total-quotas').addEventListener('keyup', function () {
id('vl_total').value = (id('total-quotas').value + id('valor6').value);
});
They are concatenating because they are strings.
If you expect those two values to be numeric, then cast them via parseInt.
parseInt(id('total-quotas').value, 10) + parseInt(id('valor6').value, 10)
Are these strings? If they are strings they will contact, if they are integers passed as strings you might need to parse as an integer
See https://www.w3schools.com/jsref/jsref_parseint.asp

How to get a Substring JAVASCRIPT? [duplicate]

This question already has answers here:
Get Substring between two characters using javascript
(24 answers)
Closed 4 years ago.
I need to iterate over strings that are inside an array, to get a sub-string in each string.
Substrings are between "()"
Something like this..
let myArray = ["animal(cat)", "color(red)", "fruits(apple)"];
//return
//'cat','red','apple'
How I could do that?
You can do this using substring and lastIndexOf functions.
let myArray = ["animal(cat)", "color(red)", "fruits(apple)"];
myArray.forEach(function(e){
console.log(e.substring(e.lastIndexOf("(") + 1, e.lastIndexOf(")")))
})

transform number with commas in number without commas [duplicate]

This question already has answers here:
Remove commas from the string using JavaScript
(3 answers)
Closed 4 years ago.
How could I transform a number with commas like 123,245 to a number without commas like this 123245, I know that you could already put commas in number without them but how to do that in reverse?
pure JAVASCRIPT please!
var number = Number("123,456".split(",").join(""));
Or:
var number = parseInt("123,456".split(",").join(""));
An alternative is using a regex /,/g
console.log("123,245".replace(/,/g, ''));
console.log("123,245,566".replace(/,/g, ''));

Displaying commas correctly [duplicate]

This question already has answers here:
How to format a number with commas as thousands separators?
(50 answers)
Closed 8 years ago.
How to add commas to numbers, presently I'm producing an output like this 1,2,3,4,5,6,7,890 - trying to have a result that outputs the following 1,234,567,890 - using keyup which might cause issues, please advise
numberWithCommas : function () {
var goal = $("#foo");
goal.val(goal.val().toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
},
Update:I found that replace(/\B(?=(\d{3})+(?!\d))/g, ','); fixed the issue of too many commas
remove all the current commas, then insert new ones in the appropriate places :
numberWithCommas : function () {
$("#foo").val(function(_,val) {
return val.replace(/\,/g,'').replace(/\B(?=(\d{3})+(?!\d))/g, ',');
});
},
FIDDLE

JavaScript window.getComputedStyle [duplicate]

This question already has answers here:
how to parse string to int in javascript
(9 answers)
Closed 8 years ago.
How can I get windows.getComputed style as an int value?
The way it is displayed by default is string:
var style = window.getComputedStyle(elem1, null);
alert(style.top); //Returns a string
parseInt() will return you the String as an Integer, so your code should look somehow like this:
var style = window.getComputedStyle(elem1, null);
alert(parseInt(style.top));
parseFloat will give you the real value as a number with decimal places preserved:
parseFloat(style.top);

Categories