How to create jQuery thousands separator while keeping .toFixed(2) [duplicate] - javascript

This question already has answers here:
Adding space between numbers
(3 answers)
Closed 4 years ago.
I'm working on a price calculation table on Wordpress using jQuery.
I want to space my numbers when the result are in thousands.
Converting this: 1000 into this: 1 000
I tried several jQuery scripts but they all got rid of my .toFixed(2)
var min = 0;
var max = 500000;
jQuery( "#input" ).keyup(function() {
if(jQuery("#input").val() < max && jQuery("#input").val() >= min && jQuery("#input").val() < 1000 ) {
var val = jQuery("#input").val() * jQuery(".price1").val();
jQuery('#amount1').html(val.toFixed(2));
}
});
At the moment the total price is displayed like this:
total price: 2349.30$
I'm trying to convert it like this:
total price: 2 349.30$

You don't need Jquery you can solve it using regular expressions...
https://stackoverflow.com/a/32889998/2894798
var min = 0;
var max = 500000;
var a = 23423;
var b = 309248023;
console.log(max.toFixed(2).toString().replace(/(?!^)(?=(?:\d{3})+(?:\.|$))/gm, ' '))
console.log(a.toFixed(2).toString().replace(/(?!^)(?=(?:\d{3})+(?:\.|$))/gm, ' '))
console.log(b.toFixed(2).toString().replace(/(?!^)(?=(?:\d{3})+(?:\.|$))/gm, ' '))

Related

Why doesn't my JavaScript program to find odd numbers work? [duplicate]

This question already has answers here:
Javascript string/integer comparisons
(9 answers)
Sum of two numbers with prompt
(10 answers)
How to force JS to do math instead of putting two strings together [duplicate]
(11 answers)
Closed 1 year ago.
I made a simple js code to input start value and end value form prompt and then find all the odd numbers, unfortunately it's not working properly. when i input 1 and 10 it'll work, but when i input 5 for sValue(start value) the program won't work. any idea?
var odd = [];
var sValue = prompt("start");
var eValue = prompt("end");
for (var i = sValue; i <= eValue; i++) {
if (i % 2 != 0) {
odd.push(i);
}
}
alert(odd);
Because the value of prompt is a string. You need to convert it to a number with parseInt(v, 10).
var odd = [];
var sValue = parseInt(prompt("start"), 10);
var eValue = parseInt(prompt("end"), 10);
for (var i = sValue; i <= eValue; i++) {
if (i % 2 != 0) {
odd.push(i);
}
}
alert(odd);

split integer add decimal point and return value in JavaScript [duplicate]

This question already has answers here:
How to round to at most 2 decimal places, if necessary
(91 answers)
Closed 2 years ago.
My integer value is like below and
var amount= 5501;
var amount= 5500;
var amount= 5560;
var amount= 5563;
var amount= 5510;
i want split this integer like below .Have to add decimal point on middle. if last digit 0 not consider.
amount and should come "55.0.1"
amount and should come "55.0"
amount and should come "55.6"
amount and should come "55.6.3"
amount and should come "55.1"
i tried this ,
var variable1 = 5500;
function versionss(variable1){
var digits = (""+variable1).split("");
if(Number(digits[3] > 0)){
return Number(variable1/100) ; //
} else {
return digits[0]+digits[1]+'.'+digits[2];
}
}
Using Number.isInteger()
function versionss(amount) {
var divisor = Number(amount) > 999 ? 100 : 10;
var value = amount / divisor;
return Number.isInteger(value) ? value.toFixed(1) : value;
}
console.log(versionss(5501));
console.log(versionss(5500));
console.log(versionss(5560));
console.log(versionss(5563));
console.log(versionss(5510));

How to round a variable value to 2 digit in javascript [duplicate]

This question already has answers here:
Pad a number with leading zeros in JavaScript [duplicate]
(9 answers)
How can I pad a value with leading zeros?
(76 answers)
Closed 5 years ago.
Suppose,
a=0;
then the result should be 00
a=10
result=10
a=2
result=02
like all values needs to round in 2 decimal point.
Note: No need to round the values having more than 2 digits.
Are you looking for something like that;
var int = 3;
var intStr = ("0" + int).slice(-2);
Output : 03
For any number of digits
var temp = 9;
if(temp < 10){
var temp = ("0" + temp).slice(-2);
}
For only two digit simply append zero if it is one digit number :-
var temp = 19;
if(temp < 10){
var temp = "0" + temp;
}

Number format with comma and decimal points [duplicate]

This question already has answers here:
How to format numbers as currency strings
(67 answers)
Closed 6 years ago.
I have tried using Number(x).toLocaleString(), but this only gives me 10,000.
When I use parseFloat(row.profit).toFixed(2) it gives me 10000.00. I tried combining parseFloat(Number(row.profit)toLocaleString()).toFixed(2) But not give me the desired output which should be 10,000.00.
How can I achieve this?
You can use a quick hack by testing if . is present in your locale string or not :
function localeFormat(x) {
var num = Number(x).toLocaleString();
if (num.indexOf("/.") > 0) {
num += ".00";
}else{
var n = parseFloat(x).toFixed(2).toString();
num = Number(n).toLocaleString();
}
return num;
}
var strs = ["10000", "10000.45", "10000.45768"];
for(var i = 0; i < strs.length; i++){
console.log(strs[i] + " -> " + localeFormat(strs[i]));
}

How to achieve this Pricing Calculator [duplicate]

This question already has answers here:
Price Calculator based on Quantity [closed]
(4 answers)
Closed 8 years ago.
I am trying to create a simple script to add to a html website.
I need it to calculate the price based on the quantity the user inputs.
For example, a value of 1-1000 will be multiplied by 1.50 and displayed, 1001-5000 multiplied by 1.20 and displayed, 5001-10000 multiplied by 1 and displayed and any number above that would display an error message like "Must be below 10000".
The result is to display in a text field so the user can click submit.
I've been trying to do this in js with no success. If this can be done in any other language please let me know. I'm still learning.
function calc(val) {
if (val < 1 || val > 10000) {
alert("Value must be a positive number under 10,000")
return 0;
}
if (val < 1001) return val*1.5;
if (val < 5001) return val*1.2;
return val;
}
This can be done trough almost every language available for web. Calculating is the most easy thing. For example, in PHP:
$output = $input - 5; // -5
$output = $input + 5; // +5
$output = $input++; // +1
$output = $input * 5; // x5
Javascript plus example:
var input = 5;
var plus = 6;
var output = input+plus;
Javascript min example:
var input = 5;
var plus = 6;
var output = input-plus;

Categories