0

I have already seen some answers in stackoverflow related type questions. I could not use "toFixed" function because after multiplying with 100 I need to get back exact value of "a" ("a" means variable). Would you please provide alternative solution to solve this issue. Given bellow added some cases for your better understanding.

//case:01
var a = 19
var b = a/100; 
console.log(b) // output: 0.19
console.log(b*100) // output: 19  as expected

//case:02
var a = 7;
var b = a/100;
console.log(b) // output: 0.07
console.log(b*100) // output: 7.000000000000001,  expected result:7, that mean not equal to variable "a"

//case:03
var a = 7.7;
var b = a/100;
console.log(b) // output: 0.077
console.log(b*100) // output: 7.7,  as expected

//case:04
var a = 7.71;
var b = a/100;
console.log(b) // output: 0.0771
console.log(b*100) // output: 7.71,  as expected

Any help would be appreciated. sorry for poor English.

Tushar Ghosh
  • 692
  • 10
  • 17
  • Welcome to floating point arithmetic – nicholaswmin Sep 28 '17 at 05:41
  • hi barmar, I have went through "Is floating point math broken?" question and answers. I don't understand how this question solve my issue and it's a duplicate question. Would you please describe how to solve the case 2 in the question. I know you have very good profile :) . – Tushar Ghosh Sep 28 '17 at 08:20
  • @TusharGhosh: You write "incorrect multiplication", but **it is not incorrect**. That is how floating point works. 7/100 is not exactly representable in binary floating point, so you get a slight difference. That is what (in many, many words) the answers to the duplicate explain. – Rudy Velthuis Sep 28 '17 at 21:04
  • @Barmar: Thanks, I got the point – Tushar Ghosh Oct 02 '17 at 02:11
  • `Math.round(b*100)` will output `7`. – Barmar Oct 02 '17 at 07:45
  • @Barmar: This is not solve the my problem bro for the case 3 and case 4. please read the question again. After multiplying with 100 I need to get back exact value of "a" ("a" means variable). Please give a exact solution for the question. Sorry for my poor English. – Tushar Ghosh Oct 02 '17 at 07:59
  • @Rudy: Thank you for your comment. – Tushar Ghosh Oct 02 '17 at 08:07
  • Many values cannot be represented "exactly" in floating point, that's the point explained in the duplicate question. You have to decide how many decimal places you care about. You can use `toFixed()` to get a string that contains that many decimal places. – Barmar Oct 02 '17 at 15:01
  • @Barmar: I have solved it by using string. During dividing point places after two digit of left side and for multiplication point move to right side. Then use parseFloat for converting string to float. It's now working as my expected. I had seen many questions and answers. They are suggesting as like you. That's why said I can not use "toFixed()" function and want a alternative answer. I think, you did not read out the question properly but marked as duplicate question very funny. By the way, I did not expect such like decision from you because you have a very good profile. – Tushar Ghosh Oct 03 '17 at 02:29
  • You can't use "exact" and "floating point" in the same sentence. The problem is that when you divide by 100, there will often be round-off error because it can't be represented exactly in binary. If you need exactness you shouldn't be using floating point in the first place. – Barmar Oct 03 '17 at 06:12
  • If I hadn't closed the question, I guarantee someone else would have used the same duplicate. This is a problem all experienced programmers are familiar with. – Barmar Oct 03 '17 at 06:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/155809/discussion-between-tushar-ghosh-and-barmar). – Tushar Ghosh Oct 03 '17 at 08:31

0 Answers0