Math Accuracy Help

Hello r/VB,

I have a bit of a math problem on my hand. I am putting together a little calculator for my home brewing adventures, to calculate potential in alcohol production. I got my formulas, and tested them in google sheets to make sure they were working. I translated them to a small VB program. Everything works… okay… with one minor problem.

If I say, have 1 + 1 ( this is a simplified version of the problem) and run it through my code, I end up with 2. On the excel sheet I also get 2. If I have 1 + 1.1, on the Excel sheet I get 2.1; in program it returns 1. If I have 1+ 1.5, in the excel sheet I get 2.5, in the program I get 2.5

I have everything set to as full a decimal place as possible. What would account for these wierd zones of refusal to update the results?

Code is as Followed:

 Private Sub calculateBtn_Click(sender As Object, e As EventArgs) Handles calculateBtn.Click Dim HoneyWeight As Decimal = (CInt(honeyTxt.Text) * 0.453592) * ((CInt(sugarTxt.Text) / 100)) Dim WaterWeight As Decimal = (CInt(waterTxt.Text) * 3.79) + (CInt(honeyTxt.Text) * ((100 - CInt(sugarTxt.Text)) / 100)) Dim TotalWeight As Decimal = (CInt(honeyTxt.Text) * 0.453592) + (CInt(waterTxt.Text) * 3.79) Dim TotalVolume As Decimal = (CInt(waterTxt.Text) * 3.79) + ((CInt(honeyTxt.Text) * 0.453592) * (1 / 1.42)) mustVolTxt.Text = Math.Round(TotalVolume, 2, MidpointRounding.AwayFromZero) Dim BottleYield As Integer = mustVolTxt.Text / (CInt(bottleSizeBx.Text) / 1000) bottleYieldTxt.Text = BottleYield Dim Brix As Decimal = HoneyWeight * 100 / WaterWeight brixTxt.Text = Brix Dim SG As Decimal = (Brix / (258.6 - ((Brix / 258.2) * 227.1))) + 1 sGTxt.Text = SG Dim FG As Decimal = CInt(fGTxt.Text) Dim ABW As Decimal = 76.08 * (SG - FG) / (1.775 - SG) Dim PABV As Integer = ABW / 0.794 PotentiolABVTxt.Text = PABV & "%" 

The only math.round I have is when calculating the volume, but that only comes into play well after the other, inaccurate, results.

Thanks in advance

submitted by /u/CityEggs
[link] [comments]

Leave a Reply