Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Decimal precision
Message
From
23/05/2012 14:06:23
 
 
To
23/05/2012 13:35:46
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01544101
Message ID:
01544174
Views:
23
>>Try this. It finds out which direction to take and exits as soon as there is a sign reversal
>>
>>
>>		static void Main(string[] args)
>>		{
>>			double totalTax, fedTax, provTax;
>>
>>
>>			totalTax = 500.00;
>>
>>			if (TrySolveTaxes(totalTax, out fedTax, out provTax))
>>				Console.WriteLine("TotalTax= {0} fedTax= {1} provTax= {2}", totalTax, fedTax, provTax);
>>			else
>>				Console.WriteLine("cannot solve");
>>
>>
>>			Console.ReadLine();
>>
>>		}
>>		public static bool TrySolveTaxes(double totalTax, out double fedTax, out double provTax)
>>		{
>>			bool success = false;
>>
>>			double fedPortion = .05;
>>			double provPortion = .095;
>>
>>			// first shot
>>			double amount = Math.Round(totalTax / (fedPortion * provPortion + fedPortion + provPortion), 2);
>>			fedTax = Math.Round(amount  * fedPortion, 2) ;
>>			provTax = Math.Round((amount  + fedTax) * provPortion, 2) ;
>>
>>
>>			int deltaSign = Math.Sign(totalTax - (fedTax + provTax));
>>
>>			if (deltaSign == 0)
>>				success = true;
>>			else
>>			{
>>				double direction = .01 * deltaSign;
>>				int oldDeltaSign = deltaSign;
>>
>>				do
>>				{
>>					amount += direction;
>>					fedTax = Math.Round(amount * fedPortion, 2);
>>					provTax = Math.Round((amount + fedTax) * provPortion, 2);
>>
>>					deltaSign = Math.Sign(totalTax - (fedTax + provTax));
>>					provTax = totalTax - fedTax;
>>
>>				} while (deltaSign == oldDeltaSign);
>>				success = true;
>>				
>>			}
>>
>>			return success;
>>		}
>>
>
>With this approach, for the totalTax of 74.88$ (correction from the 500.00$ mentioned above BTW), it falls right away in the delta and returns a base amount of 500.03$.

That is entirely possible. 500.00, 500.01, 500.02, 500.03, 500.04, 500.05, 500.06, 500.07, 500.08 and 500.09 all result in 74.88, ie 25.00 + 49.88

But the goal was to split 74.88 in 25.00 and 49.88



>The code converted to VB.NET I used is as follow:
>
>
>        TrySolveTaxes(74.88)
>
>    Private Function TrySolveTaxes(totalTax As Double) As Boolean
>        Dim fedtax As Double = 0
>        Dim provtax As Double = 0
>        Dim success As Boolean = False
>
>        Dim fedPortion As Double = 0.05
>        Dim provPortion As Double = 0.095
>
>        ' first shot
>        Dim amount As Double = Math.Round(totalTax / (fedPortion * provPortion + fedPortion + provPortion), 2)
>        fedTax = Math.Round(amount * fedPortion, 2)
>        provTax = Math.Round((amount + fedTax) * provPortion, 2)
>
>
>        Dim deltaSign As Integer = Math.Sign(totalTax - (fedTax + provTax))
>
>        If deltaSign = 0 Then
>            success = True
>        Else
>            Dim direction As Double = 0.01 * deltaSign
>            Dim oldDeltaSign As Integer = deltaSign
>
>            Do
>                amount += direction
>                fedtax = Math.Round(amount * fedPortion, 2)
>                provtax = Math.Round((amount + fedtax) * provPortion, 2)
>
>                deltaSign = Math.Sign(totalTax - (fedtax + provtax))
>
>                provtax = totalTax - fedtax
>            Loop While deltaSign = oldDeltaSign
>
>            success = True
>        End If
>
>        Return success
>    End Function
>
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform