Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TediousType.Money saving floats incorrectly #741

Open
vbanuelos opened this issue May 11, 2018 · 1 comment
Open

TediousType.Money saving floats incorrectly #741

vbanuelos opened this issue May 11, 2018 · 1 comment
Labels
known issue for any issue that has been reported or there's a PR with the fix

Comments

@vbanuelos
Copy link

I'm running into an issue where I'm sending a value of 333.34 as TediousType.Money (using addParam) and it's saving to SQL as 333.3399.

In my scenario:
Im taking the monetary amount of $1,000. Dividing that by 3 buckets, which yields 333.33. I write an entry to a SQL table (along with other properties) for that, except for third entry (since that would otherwise equal 999.99). I instead add a penny to that third entry, and then make a sql entry.

I arrive at a penny (not a magic number), by subtracting the running total before writing the last entry from the monetary amount. So Math.Round(1000-999.99*100)/100 to arrive at $0.01.

I then add the penny by

function truncateNumber(number) { return Math.trunc(number*100) / 100 }
var num1 = 333.33 * 100
var num2 = .01 * 100
var total = truncateNumber(num1 + num2) / 100       <== equals 333.34

Saving that value using TediousType.Money, to a SQL Field of type Money, will store the value as 333.3399.

My current workaround is to use TediousType.Float with precision 2, but this is undesireable as the SQL field is of type Money.

@Suraiya-Hameed
Copy link
Member

This happens because 333.34 needs to be multiplied by 10^4 before sending it to SQL Server (according to TDS spec), and 333.34 * 10000 = 3333399.9999999995 in js 😞 #678 has details on the plan to fix the datatype issues.

@Hadis-Knj Hadis-Knj added the known issue for any issue that has been reported or there's a PR with the fix label Jul 6, 2018
@IanChokS IanChokS reopened this Dec 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
known issue for any issue that has been reported or there's a PR with the fix
Projects
None yet
Development

No branches or pull requests

4 participants