Jump to content

Number(); method but with negative numbers.


crazyswede

Recommended Posts

I’m new at working with numbers. As I understand it, in ASP, by default the computer always wants to convert everything to a string, so you have to use the Number(); method. This seems to work fine, adding and subtracting numbers, until I work with negative numbers. Below is code snippet with comments on the right. EndBank = Number( ColumbiaEndBalance ); EndBank += Number( boaEndBalance); balance -= Number( EndBank ); // fs_reconcile.Write( "<td> Subtract end bals</td> \r\n" );fs_reconcile.Write( "<td> .</td> \r\n" );fs_reconcile.Write( "<td ALIGN=RIGHT> " );fs_reconcile.Write( EndBank );fs_reconcile.Write( "</td> " );fs_reconcile.Write( "<td ALIGN=RIGHT> " ); fs_reconcile.Write( balance ); <--- THIS IS a NEGATIVE NUMBER (-8779.64)fs_reconcile.Write( "</td> " );fs_reconcile.Write( " </tr> \r\n" ); BankDifference = Number( EndBank ); // BankDifference -= Number( BeginBank ); //balance += Number( BankDifference ); //<--- THIS IS SAME NUMKBER ONLY POSITIVE ……………………………………………………(8779.64) I ADD THEM.fs_reconcile.Write( "<td> Add BankDifference</td> \r\n" );fs_reconcile.Write( "<td> over the year </td> \r\n" );fs_reconcile.Write( "<td ALIGN=RIGHT> " );fs_reconcile.Write( BankDifference );fs_reconcile.Write( "</td> " );fs_reconcile.Write( "<td ALIGN=RIGHT> " ); fs_reconcile.Write( balance ); <- SHOULD BE ZERO, BUT INSTEAD ….(3.63797880709171E-12)fs_reconcile.Write( "</td> " );fs_reconcile.Write( " </tr> \r\n" );

Link to comment
Share on other sites

It looks like a precision error, this happens when working with floating point numbers.The number is really close to zero compared to the values you're working with. You can set a marginal limit in which you consider a number is zero, for example if the number is less than 0.00001. if(Math.abs(balance) < 0.00001) { balance = 0; }

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...