Decimal Sum in node js [duplicate] - node.js

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
Adding 2 numbers in nodejs with 2 decimal places doesn't leads to unexpected results
The following code results in
console.log(34.02 + 1378.12);
1412.1399999999999
But the expected result is
1412.14

You can use toFixed(2).
console.log((34.02 + 1378.12).toFixed(2));

Related

excel simple math error 1 - 1.09 = -0.0900000000000001? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
I was using a ROUNDUP functions and find a very strange answers and later i found out that 1-1.09 = -0.0900000000000001 according to excel. i am not a mathematician but i think than correct answer 1 - 1.09 must be -0.09. i cant figure it out why the answer is -0.0900000000000001. is this a bug or i have missed something?
P.S. I am using Excel2016 16.0.4417.10000 64-bit, on Windows 10
Computers only have 0 and 1 to represent any value. Converting 0.1 into a binary number would result in a long ‘zeros-and-ones-number’: .0001100011000111000111
In order to avoid an endless number, Excel would round it at the end. But this rounding of the binary equivalent to 0.1 leads to miscalculation.
found it on here. Basically excel does it to save memory https://professor-excel.com/wrong-calculations-why-does-excel-show-a-wrong-result/

Is there any method to find the lowest occurence of an character in a string as like most_common used for maximum occurence? [duplicate]

This question already has answers here:
How to get the least common element in a list?
(12 answers)
Closed 3 years ago.
string='hhelloo'
output must be 'e' because it is least occurred character in string.
lst=[1,1,2,3,4,5,5]
moc=min([(lst.count(chr),chr) for chr in (lst)])
print(moc)
You could use the last element from Counter.most_common()
from collections import Counter
print(Counter('hhelloo').most_common()[-1][0])
This wouldn't be very efficient however. For a more efficient method, take a look at this answer: https://stackoverflow.com/a/4743286/5946921
It implements a least_common function in a similar way to how Counter.most_common is implemented

How to locate a str in Matlab? [duplicate]

This question already has answers here:
How to search for a string in cell array in MATLAB?
(8 answers)
Closed 6 years ago.
I'm a newbie to Matlab and have a question about str locating:
A = ['abc','de','fghij','something','another'];
Then how can I get 3 if I use strfind(A,'fghij')?
Thanks.
I'm not exactly sure I understand your question, but if you are wondering why the value is 3 instead of 2 it is because matlab (unlike most languages you may be used to) indexes arrays starting at 1 instead of 0.
Thanks #TroyHaskin ! I find the answer in another post~
idx=find(ismemeber(A,'fghij')) is what I want.

for this statement String a="MAM"+"BCD"+"EFG"+"GFE"; How many objects will be created? (I am confused is it created 4 or 5 or 7) [duplicate]

This question already has answers here:
How many objects are created
(4 answers)
Closed 9 years ago.
for this statement
String a="MAM"+"BCD"+"EFG"+"GFE";
How many objects will be created? (I am confused is it created 4 or 5 or 7)
Most smart compilers will realize that concatenated string constants can be concatenated at compile time. If your compiler chooses to make that optimization, the answer is one.
Otherwise, you have each of your literal strings, plus one for each concatenation. Without the optimization, the answer is 7 because you have 4 strings and 3 +es.
If you're talking about Java, the answer is one, at compile time, as specified in the JLS.
If you're not, the question is unanswerable as posed.
Only one object will be created. In this case because the plus sign is used to concatenate "string literals". :)

convert double value from 1 decimal to 2 decimal places in j2me [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Rounding a double to 5 decimal places in Java ME
i have a double value which is 2.8. I was planning to display on screen in the form of 2.80. Is there any idea how to do it?
thanks=)
The DecimalFormat class, in Java SE, can handle this scenario. Unfortunately, Java ME doesn't have it or anything similar to it.
I think the solution is to convert the double value to string and add the extra '0' at the end.
String formattedValue = Double.toString(value).concat("0");

Resources