Related
I am trying to convert a string to a big integer to perform some arithmetic calculations. However, when I try this:
Number("9007199254740993")
...I am getting this unexpected result:
9007199254740992
I suspect that this is probably because of the limit on the size of integers that Number is capable of working with.
Basically, I want to check if two strings are consecutive numbers or not. Since Number is not returning the correct value, I am getting the incorrect difference for "9007199254740993" and "9007199254740992". Specifically, I am expecting 1, but getting 0.
One possibility I considered is dividing each number by a factor to make each of them smaller. Is there any other solution?
Javascript's Number type is a numeric data type in the double-precision 64-bit floating point format (IEEE 754).
If you are dealing with large integers, use a BigInt or a corresponding library.
I you don't want to rely on BigInt and only have positive integers in mind, you can also write the successor test yourself. Full code in the snippet below.
Notes
A string representation of a positive integer is easily convertible to a decimal array where the index represents the exponent to the base 10. For example "42" ~> [2, 4] (since 42 = 2*10^0 + 4*10^1). You can also just as easily convert it back.
Now for the successor test you just need to define the increment operation (which is just adding 1 with carry). With that you can just compare if the increment of one number is equal to the unincremented other number (and vice versa).
Code
// Convert a string representation of positive decimal integer to an array of decimals.
const toArray = numberString => Array.from(numberString, c => parseInt(c))
.reverse();
// Convert the array representation of a positive decimal integer string back to the corresponding string representation (this is the inverse of `toArray`).
const fromArray = numberArray => numberArray.map(String)
.reverse()
.join('');
console.log(fromArray(toArray("9007199254740993")) === "9007199254740993"); // true
// Perform the increment operation on the array representation of the positive decimal integer.
const increment = numberArray => {
let carry = 1;
const incrementedNumberArray = [];
numberArray.forEach(i => {
let j;
if (carry === 0) {
j = i;
} else if (carry === 1) {
if (i === 9) {
j = 0;
} else {
j = i + 1;
carry = 0;
}
}
incrementedNumberArray.push(j);
});
if (carry === 1) {
incrementedNumberArray.push(1);
}
return incrementedNumberArray;
};
console.log(fromArray(increment(toArray("9007199254740993"))) === "9007199254740994"); // true
console.log(fromArray(increment(toArray("9999999999999999"))) === "10000000000000000"); // true
// Test if two strings represent positive integers where one is the other's successor.
const isSuccessor = (a, b) => {
const a_ = increment(toArray(a));
const b_ = increment(toArray(b));
return fromArray(a_) === b || fromArray(b_) === a;
};
console.log(isSuccessor("9007199254740993", "9007199254740994")); // true
console.log(isSuccessor("9007199254740994", "9007199254740993")); // true
console.log(isSuccessor("9999999999999999", "10000000000000000")); // true
console.log(isSuccessor("10000000000000000", "9999999999999999")); // true
console.log(isSuccessor("10000000000000000", "10000000000000002")); // false
You can use BIG integer library like one in JAVA.
check here
npm install big-integer
var bigInt = require("big-integer");
var largeNumber1 = bigInt("9007199254740993");
var largeNumber2 = bigInt("9007199254740994"); // any other number
var ans = largeNumber1.minus(largeNumber2);
if(ans == 1 || ans == -1){
console.log('consecutive ')
}else{
console.log('not consecutive ')
}
Note: I recommend you to use BigInt(as suggested by #Andreas in comment), if you are dealing with Big Numbers.
UPDATED
Use this code to compare big positive integers(The arguments should be in string format)
function compareBigNumber(num1, num2) {
if (num1 > Number.MAX_SAFE_INTEGER && num2 > Number.MAX_SAFE_INTEGER) {
var newNum1 = num1.split('').reverse();
var newNum2 = num2.split('').reverse();
do {
newNum1.pop();
newNum2.pop();
} while (newNum1[newNum1.length-1] === '0' || newNum2[newNum2.length-1] === '0')
return compareBigNumber(newNum1.reverse().join(''), newNum2.reverse().join(''));
} else if(num1 > Number.MAX_SAFE_INTEGER){
return 'num1 is greater'
} else if (num2 > Number.MAX_SAFE_INTEGER) {
return 'num2 is greater'
}
else {
var num1Int = parseInt(num1);
var num2Int = parseInt(num2);
if (num1Int > num2Int) {
return 'Num1 is greater';
} else if (num2Int > num1Int){
return 'Num2 is greater'
} else {
return 'Num1 is equal to Num2';
}
}
}
console.log(compareBigNumber("9007199254740992", "9007199254740993"))
console.log(compareBigNumber("100000000000000000000", "0"))
So I have the following code which extracts data from an array and calculates the average. The issue is that at the moment, even if the average is 3, it appears as 3.00. What I would like is for the average to only be to 2 decimal places if necessary. Code below:
var calculated = playerdata.map((player) => {
const rounds = player.slice(2);
return {
player,
average: average(rounds).toFixed(2),
best: Math.min(...rounds),
worst: Math.max(...rounds)
};
});
function average(numbers) {
return numbers.reduce((a, b) => a + b, 0) / numbers.length;
}
You could prepend average(rounds).toFixed(2) with a +. Like
+average(rounds).toFixed(2)
Working examples:
var roundTo2 = function(num) {
return +num.toFixed(2);
}
console.log(roundTo2(3))
console.log(roundTo2(3.1))
console.log(roundTo2(3.12))
console.log(roundTo2(3.128))
UPDATE
Updating with relevant test cases
#Maaz's solution also works, but here's a solution which is more self explanatory:
average(rounds) * 100 % 1 ? average(rounds).toFixed(2) : average(rounds)
This will round only if the number goes past 2 decimal places:
f = function(a){return a * 100 % 1 ? a.toFixed(2) : a}
console.log(f(3))
console.log(f(3.1))
console.log(f(3.12))
console.log(f(3.128))
Can compare rounded integer value of number to itself to see if it has decimal using Math.round()
function printVal(num){
var isDecimal = Math.round(num) !== num;
return isDecimal ? num.toFixed(2) : num;
}
console.log(printVal(3.01));
console.log(printVal(3.1));
console.log(printVal(3));
I have a requirement to format a no to get 3 significant digit after a decimal in javascript..
detail about the significant digit can be found here http://www.usca.edu/chemistry/genchem/sigfig.htm
here are the rule for significant digit
1) ALL non-zero numbers (1,2,3,4,5,6,7,8,9) are ALWAYS significant.
2) ALL zeroes between non-zero numbers are ALWAYS significant.
3) ALL zeroes which are SIMULTANEOUSLY to the right of the decimal point AND at
the end of the number are ALWAYS significant.
4) ALL zeroes which are to the left of a written decimal point and are in a number >= 10 are ALWAYS significant.
i want function like
function significantDigit(no, noOfDecimal)
{
return signifcantNo
}
Example of significant digits.
48,923 has five significant digit..significantDigit(no,3) should return 48923
3.967 has four significant digit..significantDigit(no,3) should return 3.967
0.00104009 has six significant digit,..significantDigit(no,3) should return .00104
hope this helps
var anumber=123.45
anumber.toPrecision(6) //returns 123.450 (padding)
anumber.toPrecision(4) //returns 123.5 (round up)
anumber.toPrecision(2) //returns 1.2e+2 (you figure it out!)
thanks for the edited question
this one ll solve your requirement
var anumber = 123.4050877
var str = anumber.toPrecision(6)
var a = [];
a= JSON.parse("[" + str + "]");
alert(a.length)
for(var i=6;i<=a.length;i--){
if(a[i]=="0"){
a.splice(i, 1);
}
}
alert(a)
i have found a java code here thanks to Pyrolistical
Rounding to an arbitrary number of significant digits
public static double roundToSignificantFigures(double num, int n) {
if(num == 0) {
return 0;
}
final double d = Math.ceil(Math.log10(num < 0 ? -num: num));
final int power = n - (int) d;
final double magnitude = Math.pow(10, power);
final long shifted = Math.round(num*magnitude);
return shifted/magnitude;}
i have converted this to a javascript code, this can be found at http://jsfiddle.net/f6hdvLjb/4/
javascript code is
function roundToSignificantFigures(num, n) {
if(num === 0) {
return 0;
}
var d = Math.ceil(Math.log10(num < 0 ? -num: num));
var power = n - parseInt(d);
var magnitude = Math.pow(10, power);
var shifted = Math.round(num*magnitude);
alert(shifted/magnitude);
return shifted/magnitude;
}
roundToSignificantFigures(6666666.0412222919999,3);
i think this is what the significant digit logic.
this may not be the complete solution..but its correct to the most extent (i think) it works really great for very big decimal no..this will give you most significant digit after decimal
function signiDigit(val, noOfdecimalPoint) {
debugger;
var noString = String(val);
var splitNo = noString.split(".");
if (splitNo.length > 1) {
if(parseInt(splitNo[0])!==0 ||splitNo[0]==='' )
{
if(noString.length - 1 > noOfdecimalPoint)
{
return Math.round(val);
}else
{
return val;
}
}else
{
var noafterDecimal =String(parseInt(splitNo[1]));
if(noafterDecimal.length > noOfdecimalPoint)
{
return parseFloat(val.toFixed(splitNo[1].indexOf(noafterDecimal) + noafterDecimal.length-1));
}
else{
return val;
}
}
}}
var no = signiDigit(9.999,3);
alert(no);
here is the fiddeler link http://jsfiddle.net/n1gt4k90/4/
this is not the complete significant no but mix of significant and rounding.
I need to add zeroes, so that each number has at least two decimals, but without rounding. So for example:
5 --> 5.00
5.1 --> 5.10
5.11 --> 5.11 (no change)
5.111 --> 5.111 (no change)
5.1111 --> 5.1111 (no change)
My function is missing an IF to check for less than two decimal places:
function addZeroes( num ) {
var num = Number(num);
if ( //idk ) {
num = num.toFixed(2);
}
return num;
}
Thanks!
Posting an alternative answer, in addition to the two below. (Keep in mind that I'm no expert and this is just for text inputs, not for parsing complex values like colors that could have floating point issues, etc.)
function addZeroes( value ) {
//set everything to at least two decimals; removs 3+ zero decimasl, keep non-zero decimals
var new_value = value*1; //removes trailing zeros
new_value = new_value+''; //casts it to string
pos = new_value.indexOf('.');
if (pos==-1) new_value = new_value + '.00';
else {
var integer = new_value.substring(0,pos);
var decimals = new_value.substring(pos+1);
while(decimals.length<2) decimals=decimals+'0';
new_value = integer+'.'+decimals;
}
return new_value;
}
[This is not a duplicate question. The question you linked assumes "knowing that they have at least 1 decimal." Decimal points cannot be assumed in text inputs, and this was making errors.]
Here you go:
function addZeroes(num) {
// Convert input string to a number and store as a variable.
var value = Number(num);
// Split the input string into two arrays containing integers/decimals
var res = num.split(".");
// If there is no decimal point or only one decimal place found.
if(res.length == 1 || res[1].length < 3) {
// Set the number to two decimal places
value = value.toFixed(2);
}
// Return updated or original number.
return value;
}
// If you require the number as a string simply cast back as so
var num = String(value);
See fiddle for demonstration.
edit: Since I first answered this, javascript and I have progressed, here is an improved solution using ES6, but following the same idea:
function addZeroes(num) {
const dec = num.split('.')[1]
const len = dec && dec.length > 2 ? dec.length : 2
return Number(num).toFixed(len)
}
Updated fiddle
edit 2: Or if you are using optional chaining you can do it in one line like so:
const addZeroes = num => Number(num).toFixed(Math.max(num.split('.')[1]?.length, 2) || 2)
Updateder fiddle
Maybe use .toLocaleString():
var num = 5.1;
var numWithZeroes = num.toLocaleString("en",{useGrouping: false,minimumFractionDigits: 2});
console.log(numWithZeroes);
As a function/demo:
function addZeroes(num) {
return num.toLocaleString("en", {useGrouping: false, minimumFractionDigits: 2})
}
console.log('before after correct');
console.log('5 ->', addZeroes(5) , ' --> 5.00');
console.log('5.1 ->', addZeroes(5.1) , ' --> 5.10');
console.log('5.11 ->', addZeroes(5.11) , ' --> 5.11 (no change)');
console.log('5.111 ->', addZeroes(5.111) , ' --> 5.111 (no change)');
console.log('5.1111 ->', addZeroes(5.1111) , '--> 5.1111 (no change)');
console.log('-5 ->', addZeroes(-5) , ' --> -5.00');
And if you must use .toFixed(), here's a one-liner:
var num = 5.1;
var numWithZeroes = num.toFixed(Math.max(((num+'').split(".")[1]||"").length, 2));
console.log(numWithZeroes);
Or, again, as a function/demo:
function addZeroes(num) {
return num.toFixed(Math.max(((num+'').split(".")[1]||"").length, 2));
}
console.log('before after correct');
console.log('5 ->', addZeroes(5) , ' --> 5.00');
console.log('5.1 ->', addZeroes(5.1) , ' --> 5.10');
console.log('5.11 ->', addZeroes(5.11) , ' --> 5.11 (no change)');
console.log('5.111 ->', addZeroes(5.111) , ' --> 5.111 (no change)');
console.log('5.1111 ->', addZeroes(5.1111) , '--> 5.1111 (no change)');
console.log('-5 ->', addZeroes(-5) , ' --> -5.00');
The below code provides one way to do what you want. There are others.
function addZeroes(num) {
// Cast as number
var num = Number(num);
// If not a number, return 0
if (isNaN(num)) {
return 0;
}
// If there is no decimal, or the decimal is less than 2 digits, toFixed
if (String(num).split(".").length < 2 || String(num).split(".")[1].length<=2 ){
num = num.toFixed(2);
}
// Return the number
return num;
}
console.log(addZeroes(5)); // Alerts 5.00
console.log(addZeroes(5.1)); // Alerts 5.10
console.log(addZeroes(5.11)); // Alerts 5.11
console.log(addZeroes(5.111)); // Alerts 5.111
http://jsfiddle.net/nzK4n/
decimalNumber = number => Number.isInteger(number) ? number.toFixed(2) : number
Here is a function that will do this, function expects a number
var addZeroes = function(num) {
var numberAsString = num.toString();
if(numberAsString.indexOf('.') === -1) {
num = num.toFixed(2);
numberAsString = num.toString();
} else if (numberAsString.split(".")[1].length < 3) {
num = num.toFixed(2);
numberAsString = num.toString();
}
return numberAsString
};
For what is worth, this is my recursive solution to this:
const addZeros = (decimal, value, check = true) => {
if (check && decimal <= value.length) return value;
if (decimal <= 0) return value;
const newValue = value.length <= decimal ? '0' + value : value;
return addZeros(decimal - 1, newValue, false);
};
decimal is the number of decimal you want
value is the value you want
check is not suppose to be set, it's here to prevent some issue in the first call.
e.g:
addZeros(3, "3") ==> "003"
addZeros(3, "30") ==> "030"
addZeros(3, "300") ==> "300"
addZeros(3, "3000") ==> "3000"
this solution checks if the number is fixed
decimalNumber = number => Number.isInteger(number) && number % 1 === 0 ? number : number.toFixed(4);
For number type textbox
Append .00 if number present
function addZeroes(ev) {
debugger;
// Convert input string to a number and store as a variable.
var value = Number(ev.value);
// Split the input string into two arrays containing integers/decimals
var res = ev.value.split(".");
// If there is no decimal point or only one decimal place found.
if (res.length == 1 || res[1].length < 3) {
// Set the number to two decimal places
value = value.toFixed(2);
}
// Return updated or original number.
if (ev.value != "") {
ev.value = String(value);
}
}
<input type="number" step=".01" onchange="addZeroes(this)" />
We can solve this using pipe in angular. we will pass digit information parameter to decimal pipe and see how it works -
Digit Info Parameter (3.2-5):
{{ decimal_value | number:'3.2-5' }}
In the above code we are instructing decimal pipe to show atleast 3 integer values before decimal points and minimum 2 fraction digit, maximum 5 fraction digits.
if decimal_value = 5.123 then it will print 005.12300
if decimal_value = 53.1 then it will print 053.10
Work for me -
let a = 12
console.log(a.toLocaleString("en", {useGrouping: false, minimumFractionDigits: 2}))
output - 12.00
I am looking for an easy way in JavaScript to check if a number has a decimal place in it (in order to determine if it is an integer). For instance,
23 -> OK
5 -> OK
3.5 -> not OK
34.345 -> not OK
if(number is integer) {...}
Using modulus will work:
num % 1 != 0
// 23 % 1 = 0
// 23.5 % 1 = 0.5
Note that this is based on the numerical value of the number, regardless of format. It treats numerical strings containing whole numbers with a fixed decimal point the same as integers:
'10.0' % 1; // returns 0
10 % 1; // returns 0
'10.5' % 1; // returns 0.5
10.5 % 1; // returns 0.5
Number.isInteger(23); // true
Number.isInteger(1.5); // false
Number.isInteger("x"); // false:
Number.isInteger() is part of the ES6 standard and not supported in IE11.
It returns false for NaN, Infinity and non-numeric arguments while x % 1 != 0 returns true.
Or you could just use this to find out if it is NOT a decimal:
string.indexOf(".") == -1;
Simple, but effective!
Math.floor(number) === number;
The most common solution is to strip the integer portion of the number and compare it to zero like so:
function Test()
{
var startVal = 123.456
alert( (startVal - Math.floor(startVal)) != 0 )
}
Number.isSafeInteger(value);
In JavaScript, isSafeInteger() is a Number method that is used to return a Boolean value indicating whether a value is a safe integer. This means that it is an integer value that can be exactly represented as an IEEE-754 double precision number without rounding.
//How about byte-ing it?
Number.prototype.isInt= function(){
return this== this>> 0;
}
I always feel kind of bad for bit operators in javascript-
they hardly get any exercise.
Number.isInteger() is probably the most concise. It returns true if it is an integer, and false if it isn't.
number = 20.5
if (number == Math.floor(number)) {
alert("Integer")
} else {
alert("Decimal")
}
Pretty cool and works for things like XX.0 too!
It works because Math.floor() chops off any decimal if it has one so if the floor is different from the original number we know it is a decimal! And no string conversions :)
var re=/^-?[0-9]+$/;
var num=10;
re.test(num);
convert number string to array, split by decimal point. Then, if the array has only one value, that means no decimal in string.
if(!number.split(".")[1]){
//do stuff
}
This way you can also know what the integer and decimal actually are. a more advanced example would be.
number_to_array = string.split(".");
inte = number_to_array[0];
dece = number_to_array[1];
if(!dece){
//do stuff
}
function isDecimal(n){
if(n == "")
return false;
var strCheck = "0123456789";
var i;
for(i in n){
if(strCheck.indexOf(n[i]) == -1)
return false;
}
return true;
}
parseInt(num) === num
when passed a number, parseInt() just returns the number as int:
parseInt(3.3) === 3.3 // false because 3 !== 3.3
parseInt(3) === 3 // true
Use following if value is string (e.g. from <input):
Math.floor(value).toString() !== value
I add .toString() to floor to make it work also for cases when value == "1." (ends with decimal separator or another string). Also Math.floor always returns some value so .toString() never fails.
Here's an excerpt from my guard library (inspired by Effective JavaScript by David Herman):
var guard = {
guard: function(x) {
if (!this.test(x)) {
throw new TypeError("expected " + this);
}
}
// ...
};
// ...
var number = Object.create(guard);
number.test = function(x) {
return typeof x === "number" || x instanceof Number;
};
number.toString = function() {
return "number";
};
var uint32 = Object.create(guard);
uint32.test = function(x) {
return typeof x === "number" && x === (x >>> 0);
};
uint32.toString = function() {
return "uint32";
};
var decimal = Object.create(guard);
decimal.test = function(x) {
return number.test(x) && !uint32.test(x);
};
decimal.toString = function() {
return "decimal";
};
uint32.guard(1234); // fine
uint32.guard(123.4); // TypeError: expected uint32
decimal.guard(1234); // TypeError: expected decimal
decimal.guard(123.4); // fine
You can multiply it by 10 and then do a "modulo" operation/divison with 10, and check if result of that two operations is zero. Result of that two operations will give you first digit after the decimal point.
If result is equal to zero then the number is a whole number.
if ( (int)(number * 10.0) % 10 == 0 ){
// your code
}
function isDecimal(num) {
return (num !== parseInt(num, 10));
}
You can use the bitwise operations that do not change the value (^ 0 or ~~) to discard the decimal part, which can be used for rounding. After rounding the number, it is compared to the original value:
function isDecimal(num) {
return (num ^ 0) !== num;
}
console.log( isDecimal(1) ); // false
console.log( isDecimal(1.5) ); // true
console.log( isDecimal(-0.5) ); // true
function isWholeNumber(num) {
return num === Math.round(num);
}
When using counters with decimal steps, checking if number is round will actually fail, as shown below. So it might be safest (although slow) to format the number with 9 (could be more) decimal places, and if it ends with 9 zeros, then it's a whole number.
const isRound = number => number.toFixed(9).endsWith('000000000');
for (let counter = 0; counter < 2; counter += 0.1) {
console.log({ counter, modulo: counter % 1, formatted: counter.toFixed(9), isRound: isRound(counter) });
}
Perhaps this works for you?
It uses regex to check if there is a comma in the number, and if there is not, then it will add the comma and stripe.
var myNumber = '50';
function addCommaStripe(text){
if(/,/.test(text) == false){
return text += ',-';
} else {
return text;
}
}
myNumber = addCommaStripe(myNumber);
You can use this:
bool IsInteger() {
if (num.indexOf(".") != -1) // a decimal
{
return Math.ceil(num) == Math.floor(num); // passes for 1.0 as integer if thats the intent.
}
return Number.isSafeInteger(num);
}
to check if the number is integer or decimal.
Using Number.isInteger(num) can help check what would count as whole number and what would not.
For example:
let num1 = 6.0000000000000001; // 16 decimal places
let num2 = 6.000000000000001; // 15 decimal places
Number.isInteger(num1); // true, because of loss of precision
// while:
Number.isInteger(num2); // false
So, in my opinion it's safe to use Number.isInteger() over other suggested ways if what you need is to know what is an integer mathematically.
Function for check number is Decimal or whole number
function IsDecimalExist(p_decimalNumber) {
var l_boolIsExist = true;
if (p_decimalNumber % 1 == 0)
l_boolIsExist = false;
return l_boolIsExist;
}