This question already has answers here:
convert '1' to '0001' in JavaScript [duplicate]
(4 answers)
Closed 9 years ago.
I'm trying to format a variable to have a specific format 0000.00 currently my variable is be returned as 1.00 and want to get 0001.00
Any help would be greatly appreciated!
Here is a really short function to do what you want:
function formatNum(num) {
return ('0000'+num.toFixed(2)).slice(-7);
}
Demo: http://jsfiddle.net/DuZqk/
I use a similar method for integers, but i assume you are dealing with a string as javascript return 1.00 as 1
function pad(num, size) {
var f=num.split(".");
while (f[0].length < size) f[0] = "0" + f[0];
return f[0]+f[1];
}
I touched on this topic once and have this function laying around. Perhaps that is of use for you.
Just call the function with your number and the digits you want.
function pad(num, digits) {
var padding = '';
var numdigits = num.toString();
var parts = numdigits.split(".");
if (parts[1].length < 2) {
numdigits = numdigits + "0";
}
if (numdigits.length > digits) {
warning("length number is longer than requested digits");
return;
} else {
for (var i = 0; i < digits; i++) {
padding += "0";
}
var numstr = padding + numdigits;
numstr = numstr.substr(numdigits.length);
return numstr;
}
}
call:
pad(1.50, 6);
result "001.50"
fiddle: http://jsfiddle.net/djwave28/gKs3U/8/
Related
This question already has answers here:
How do you reverse a string in-place in JavaScript?
(57 answers)
Closed 6 months ago.
When I write same logic in C programming it reverse the string but in Javascript it is not working, Can somebody help to figure out why? Thanks in advance.
function reverse(str) {
let res=str;
for(let i = 0; i < str.length/2; i++) {
let temp = res[i];
res[i] = res[res.length-i-1];
res[res.length-i-1] = temp;
}
return res;
}
console.log(reverse("anil"))
Strings in JavaScript are immutable.
You can read a character from a position with foo[index] but you can't write to it.
Convert the string to an array of single-character strings with split. Then convert it back to a string (with join) at the end.
function reverse(str) {
let res = str.split("");
for (let i = 0; i < str.length / 2; i++) {
let temp = res[i];
res[i] = res[res.length - i - 1];
res[res.length - i - 1] = temp;
}
return res.join("");
}
console.log(reverse("anil"))
That said. Arrays have a reverse method, so you don't need to reinvent the wheel.
function reverse(str) {
let res = str.split("");
res.reverse();
return res.join("");
}
console.log(reverse("anil"))
This question already has answers here:
Rounding numbers to 2 digits after comma
(10 answers)
Closed 3 years ago.
Let's say we have the number 300 and I wanted it to be padded to end as 300.000
Or the number 23,5 would be something like 23.500
Hope it will help, details are in comments.
function formating(n) {
// Convert number into a String
const str = n.toString();
// Find the position of the dot.
const dotPosition = str.search(/\./);
// Return string with the pad.
if (dotPosition === -1) {
return str += ".000";
} else {
const [part1, part2] = str.split('.');
return part1 + '.' + part2.padEnd(3, "0");
}
}
This question already has answers here:
How to format a number with commas as thousands separators?
(50 answers)
Large numbers erroneously rounded in JavaScript
(6 answers)
Closed 6 years ago.
The first thing i want to ask is if there is a more efficient way of writing this programm of mine. The second question is why on earth after a specific number of characters the programm does not function well and prints zeros.
function toCurrency(price) {
var currencyToString = price.toString();
var finalPrice = "";
var counterComma = 0;
for (var i = 0; i < currencyToString.length; i++) {
counterComma++;
finalPrice += currencyToString[i];
if(counterComma == 3){
counterComma = 0;
finalPrice += ",";
}
}
return finalPrice;
}
console.log(toCurrency(123456253635423197874));
https://jsfiddle.net/DimitriXd4/68epen4n/
This question already has answers here:
Does JavaScript have a method like "range()" to generate a range within the supplied bounds?
(88 answers)
Closed 7 years ago.
I want to know if it is possible to make a method that takes two integer values as parameters and returns them and all the numbers between then in an array.
So for example if my method is
function getNumberRange(first, last)
And i call it
getNumberRanger(10, 13)
Is there a way for me to have the answer returned as the following array value
[10, 11, 12, 13]
Thanks in advance and sorry if this is badly worded.
Of course it is.
function getNumberRange(first, last) {
var arr = [];
for (var i = first; i <= last; i++) {
arr.push(i);
}
return arr;
}
You may even want to add a check to make sure first is indeed last than greatest though to avoid errors. Maybe something like this:
if (first > last) {
throw new Error("first must be less than last");
}
Similar answer that handles sorting high and low
function makeArray(n1, n2) {
var high = n1;
var low = n2;
if (n1 < n2) {
high = n2;
low = n1;
}
var myArray = [];
for(var i=0; i < (high - low) + 1; i++) {
myArray.push(low + i);
}
return myArray;
This question already has answers here:
How to avoid scientific notation for large numbers in JavaScript?
(27 answers)
Closed 6 years ago.
e.g.Number.MAX_VALUE.toString() is "1.7976931348623157e+308"
I hope there is no e+308,How to achieve this ?
You could do like this:
var n = Number.MAX_VALUE.toString();
var parts = n.split("e+");
var first = parts[0].replace('.', "");
var zeroes = parseInt(parts[1], 10) - (first.length - 1);
for(var i = 0; i < zeroes; i++){ first += "0"; }
// => first === "179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"