convert big number to string without scientific notation [duplicate] - javascript

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"

Related

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

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

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

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/

Java Script - Extract number from string [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How might I extract the number from a number + unit of measure string using JavaScript?
How to extract number from string like this in JS.
String: "Some_text_123_text" -> 123
JSFiddle Demo
var s = "Some_text_123_text";
var index = s.match(/\d+/);
document.writeln(index);​
try this
var string = "Some_text_123_text";
var find = string.split("_");
for(var i = 0; i < find.length ; i ++){
if(!isNaN(Number(find[i]))){
var num = find[i];
}
}
alert(num);
try this working fiddle
var str = "Some_text_123_text";
var patt1 = /[0-9]/g;
var arr= str.match(patt1);
var myval = arr.join("");

Categories

Resources