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;
}
Related
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);
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));
This question already has answers here:
How to format numbers? [duplicate]
(17 answers)
Closed 3 years ago.
I'm trying to create a function that will format a number received from the user's input so that it will add commas in the right places. For example, a 1000 becomes a 1,000. A 1000000 becomes a 1,000,000 an so on.
var formatNumber = function (num) {
var numSplit, dec, int;
num = Math.abs(num);
num = num.toFixed(2);
numSplit = num.split('.');
int = numSplit[0];
dec = numSplit[1];
if (int.length > 3 && int.length <= 6) {
int = int.substr(0, int.length - 3) + ',' + int.substr(int.length - 3, 4);
} else if (int.length > 6) {
int = int.substr(0, int.length - 6) + ',' + int.substr(int.length - 6, int.length - 4) + ',' + int.substr(int.length - 3, 7);
}
return int + '.' + dec
}
The function works great for numbers with up to 7 digits. 1 million turns into 1,000,000.00 perfectly, but above that it adds another zero, so a 10 million becomes 10,0000,000.00 . How can this be fixed?
Here's a codepen: https://codepen.io/samivino/pen/ZEzRjjy
P.s.- Numbers higher than 999,999,999 are not relevant, so there's no need to add more that 2 commas.
You can simply use toLocaleString()
var number = 1000000;
var formatted = number.toLocaleString();
console.log(formatted) //1,000,000
Read here for more information on how to use it.
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]));
}
This question already has an answer here:
Javascript Adding Two Decimal Places
(1 answer)
Closed 9 years ago.
I have a float,
var a = 324620.8
and I want it to look like this
a = 324620.80
This is my code so far,
var a_float = a;
var a_int = parseInt(a);
d = a_float - a_int;
if(d <= 0){
a = a_int+'.00';
}else{
if(d < 0 && d > 0.1){
a = a_int + d + '0';
}else{
a = a_float;
}
}
This would works for only one decimal digit.
I want it to work when I have 2 decimal digits.
.toFixed would not work in some browsers.
Answering the question in the title
How to find how many decimal digits in a float?
Compare position of '.' to length of float as a String.
var x = 1.2345,
x_str = x.toString(),
decimal_digits = x_str.length - x_str.lastIndexOf('.') - 1;
decimal_digits === x_str.length && (decimal_digits = 0); // case no decimal
decimal_digits; // 4
JSFIDDLE
Use toFixed("2");
var f = 1.3454545;
f.toFixed(2);
var decimal = 4.0;
var a = (decimal).toFixed(2);
console.log(a); // outputs 4.00