Number format with comma and decimal points [duplicate] - javascript

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]));
}

Related

Why my method is not reversing the string [duplicate]

This question already has answers here:
How do you reverse a string in-place in JavaScript?
(57 answers)
Are JavaScript strings immutable? Do I need a "string builder" in JavaScript?
(10 answers)
How do I split a string into an array of characters? [duplicate]
(8 answers)
Closed 8 days ago.
I know there are many other ways to reverse a string in JS but I wrote this and it is not working and I want to understand why. Mine only has two extra parameters so I can tell it to reverse from here to there.
function strRev(str, startRev, endRev) {
while (startRev < endRev) {
let temp = str[startRev];
str[startRev] = str[endRev];
str[endRev] = temp;
startRev += 1;
endRev -= 1;
}
return str;
}
And usage:
let str = "STACK";
strRev(str, 0, str.length -1 );
But what I get as result is the same original string. I don't understand why.
It works when I trace it on paper.
You can not set the character of a string using the index with bracket notation.
To do what you are trying to do, you need to use an array and not a string.
function strRev(orgStr, startRev, endRev) {
const str = Array.from(orgStr); // orgStr.split('');
while (startRev < endRev) {
let temp = str[startRev];
str[startRev] = str[endRev];
str[endRev] = temp;
startRev += 1;
endRev -= 1;
}
return str.join('');
}
let str = "STACK";
console.log(strRev(str, 0, str.length - 1));
Your algorithm is correct, but you need to change how you modify the string
Unfortunately you can't poke individual characters into a string, like you are trying to do.
Doubly unfortunately, trying to do so does not cause an error in Javascript.
function strRev(str, startRev, endRev) {
while (startRev < endRev) {
str = str.slice(0, startRev) + str[endRev] + str.slice(startRev + 1, endRev) + str[startRev] + str.slice(endRev + 1)
startRev += 1;
endRev -= 1;
}
return str;
}
let str = "STACK";
console.log(strRev(str, 0, str.length - 1));

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);

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;
}

JavaScript - Formating a long integer [duplicate]

This question already has answers here:
How to format a number with commas as thousands separators?
(50 answers)
Closed 8 years ago.
How can I take a JavaScript integer of arbitrary length, such as 1234567890, and format it as a string "1,234,567,890"?
You can use toLocaleString() for the format that you have asked.
var myNum = 1234567890;
var formattedNum = myNum.toLocaleString();
The best way is probably with a regular expression. From How to print a number with commas as thousands separators in JavaScript:
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
My solution:
var number = 1234567890;
var str = number + "";
var result = str.split('').map(function (a, i) {
if ((i - str.length) % 3 === 0 && i !== 0) {
return ',' + a;
} else {
return a;
}
}).join('');
See fiddle.

Display Random Number by Javascript [duplicate]

This question already has answers here:
Generating random whole numbers in JavaScript in a specific range
(39 answers)
Closed 9 years ago.
I'm following:
window.onload = generateRandomNumber;
function generateRandomNumber(){
var n = 25;
var number = Math.floor(Math.random()*n)+1;
document.getElementById("randomNumber").innerHTML = number;
}
Now I want to show a random number that from 20 to 25. How can I do that?
Thanks!
Just try:
var n = Math.floor(Math.random()*5) + 1;
n += 20;
The n value will be between 20 and 25...

Categories