NodeJS How to pad the end of numbers with . and zeros? [duplicate] - javascript

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

Related

replace substring between two substrings on every match [duplicate]

This question already has answers here:
Regular expression: same character 3 times
(3 answers)
Closed 2 years ago.
Is there a faster built-in function in JavaScript to replace a substring2 between two substrings with a sequence of numbers the length of substring2, than looping the whole string and replace it by hand.
example:
substring before: "before"
substring after: "after"
if substring2 is length of 3 => replace with string 011
if substring2 is length of 6 => replace with string 999999
string:
"beforeoooafter beforeaftebefore123456afteradf"
ooo would be the substring2 and 123456 too
ooo => 011 (because length 3)
123456 => 999999 (because length 6)
substring2 is a match between the string before and after
result:
"before011after beforeaftebefore999999afteradf"
You can use a Regular Expression and a replace function:
const input = "beforeoooafter beforeaftebefore123456afteradf";
const expectedOutput = "before011after beforeaftebefore999999afteradf";
const output = input.replace(/(before(?:(?!before|after).)*after)/g, function(m) {
const before = 'before';
const after = 'after';
const middle = m.substr(before.length, m.length - after.length - before.length);
if (middle.length === 3) {
return before + '011' + after;
} else if (middle.length === 6) {
return before + '999999' + after;
}
return m;
});
console.log(output);
console.log(output === expectedOutput);

Separating every 3 numbers with commas [duplicate]

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/

How do i split an integer and all the digits produced to create a new number? [duplicate]

This question already has answers here:
JavaScript Number Split into individual digits
(30 answers)
Closed 6 years ago.
For instance, if i have the number 4444. I need it to do 4+4+4+4=16.
i want to get the result of 16. I tried changing the number to a string and got the array. But i don't know how to add it after. I searched it up but the other examples were too complicated for my level.
Working Example
var n = 4444;
var a = n.toString().split('').map(Number).reduce(function(a, b) {
return a + b;
});
Turn number to string
Split it
Map over array to return Numbers
Reduce to get total
or with a loop:
var sum = 0;
var string = n.toString();
for (var i = 0; i < string.length; i++) {
sum = sum + Number(string[i]);
}

convert big number to string without scientific notation [duplicate]

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"

Formatting Currency Amount To 0000.00 with jQuery/JavaScript [duplicate]

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/

Categories

Resources