Ram V
Ram V

Reputation: 734

javascript losing decimals during assignment

I am trying to do a simple thing but for some reason it is not working. I am using Knockout and I have a model which I update after user enter some data and use the same to communicate back to C# code on server side. For some reason, when I try to assign decimal value to one of the member of model it isn't working. Though, in this case I am using knockout, I believe it has nothing to do with KO. See the screenshot where I have the value 22.78 and I am trying to do parseFloat but it ends up as just 22. I tried other things such as removing he parseFloat just to see if it accepts the string value as it is but even that is not working. Can someone help?

Screenshot

Upvotes: 0

Views: 79

Answers (2)

Noor
Noor

Reputation: 31

try this (please notice the double || )

this.AMOUNT_RECEIVED = parseFloat(data.AMOUNT_RECEIVED) || 0;

Upvotes: 2

balachandar
balachandar

Reputation: 825

Your doing Bitwise OR while assigning the value.

this.AMOUNT_RECEIVED = parseFloat(data.AMOUNT_RECEIVED) | 0;

so only it returns 22. Because 22.78 | 0 is 22. Please check this code.

console.log(22.78 | 0);

Please try this code while assigning value. You can get decimal values without loss.

this.AMOUNT_RECEIVED = parseFloat(data.AMOUNT_RECEIVED);

Pelase Check below link for more details. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_OR

Upvotes: 2

Related Questions