Click to See Complete Forum and Search --> : Adding Decimal Values


ASPSQLVB
01-15-2007, 10:26 PM
Applying Pounds and Ounces, is there a way to add
299.00
149.15
144.15

and get this exact answer.....

523.14 ?

523 LBS 14 OUNCES

Terrorke
01-16-2007, 01:06 AM
number = "523.14"
arrnum = split(number,".")
arrnum(0) gives you 523
arrnum(1) gives you 14

The number variabel has to be a string. I'm pretty sure there is also a function to do this on a numeric value.

ASPSQLVB
01-16-2007, 11:29 AM
Thanks for replying but, I was looking for a function that could add the ounces and carry over the pounds.
For Example:.. 299.00 + 149.15 + 144.15 = 592.30

592.30 is the wrong answer. I am looking to get 593.14

Every 16 ounces should be carried over to the pounds column.
In this case 1 pound gets carried over making 592....593 and taking the 1 pound (16 oz) from the ounces column would then leave 14.
Answer being 593.14.

russell
01-17-2007, 12:58 AM
this could be done in less code (1 line), but i broke it up into several lines to help understand the process.

since the lbs aren't in base 16, we need to seperate the lbs from the ounces and do two calculations, carrying the digits ourselves.

Dim a, b, c
Dim intPart
Dim decimalPart
Dim extraLBs
Dim extraOZs
Dim result

a = 299
b = 149.15
c = 144.15


intPart = fix(a) + fix(b) + fix(c)
decimalPart = Round(((a + b + c) - intPart) * 100)

extraLBs = fix(decimalPart / 16)
extraOZs = decimalPart mod 16

result = (intPart + extraLBs) & "." & extraOZs

Response.Write result


can easily make this a function which can accept an array of nums too...

hope this helps

cheers
russell

ASPSQLVB
01-17-2007, 02:04 PM
Russell,

Once again very impressive!!.....I had to write some code kind of similiar to what you posted but, a little more in depth. Its a little longer but does the job.

I have to calculate ounces that would not exceed 45 and there are 3 different pound values I would have to add. Also, the datatype is a decimal which sometimes display .1 instead of .10....I need the zero concatenated. This might not make too much sense. Also, I like writing the code in Visual Basic and line stepping through the logic. This helps tremendously.Anyway below is the code I am using.

Dim z, TotalWeightSum
For z = 1 To Len(RS3("TotalWeight")) - 1
If Mid(RS3("TotalWeight"), z, 1) = "." Then
TotalWeightOz = Mid(RTrim(RS3("TotalWeight")), z + 1)
TotalWeightLbs = Left(RS3("TotalWeight"), z - 1)
If TotalWeightOz = 16 Then
TotalWeightLbs = CInt(TotalWeightLbs) + 1
TotalWeightLbs = TotalWeightLbs & ".00"
Exit For
End If
If TotalWeightOz = "15" Or TotalWeightOz = "14" Or TotalWeightOz = "13" Or TotalWeightOz = "12" Or TotalWeightOz = "11" Or TotalWeightOz = "10" Or TotalWeightOz = "09" Or TotalWeightOz = "08" Or TotalWeightOz = "07" Or TotalWeightOz = "06" Or TotalWeightOz = "05" Or TotalWeightOz = "04" Or TotalWeightOz = "03" Or TotalWeightOz = "02" Or TotalWeightOz = "01" Then
TotalWeightLbs = CInt(TotalWeightLbs) & "." & TotalWeightOz
Exit For
End If
If TotalWeightOz = "00" Then
TotalWeightLbs = CInt(TotalWeightLbs) & "." & CInt(TotalWeightOz)
Exit For
End If
If TotalWeightOz = "1" Or TotalWeightOz = "2" Or TotalWeightOz = "3" Or TotalWeightOz = "4" Then
TotalWeightOz = TotalWeightOz & "0"
If TotalWeightOz = "10" Then
TotalWeightLbs = CInt(TotalWeightLbs) & "." & TotalWeightOz
Exit For
End If
TotalWeightOzTimes = TotalWeightOz / 16
TotalWeightLbs = Int(TotalWeightLbs) + Int(TotalWeightOzTimes)
TotalweightOzExtra = Int(TotalWeightOz) Mod 16
If TotalweightOzExtra = "15" Or TotalweightOzExtra = "14" Or TotalweightOzExtra = "13" Or TotalweightOzExtra = "12" Or TotalweightOzExtra = "11" Or TotalweightOzExtra = "10" Then
TotalWeightLbs = CInt(TotalWeightLbs) & "." & TotalweightOzExtra
Exit For
End If
If TotalweightOzExtra = "9" Or TotalweightOzExtra = "8" Or TotalweightOzExtra = "7" Or TotalweightOzExtra = "6" Or TotalweightOzExtra = "5" Or TotalweightOzExtra = "4" Or TotalweightOzExtra = "3" Or TotalweightOzExtra = "2" Or TotalweightOzExtra = "1" Or TotalweightOzExtra = "0" Then
TotalWeightLbs = CInt(TotalWeightLbs) & ".0" & TotalweightOzExtra
Exit For
End If
End If

If TotalWeightOz > "16" Then
TotalWeightOzTimes = TotalWeightOz / 16
TotalWeightLbs = Int(TotalWeightLbs) + Int(TotalWeightOzTimes)
TotalweightOzExtra = Int(TotalWeightOz) Mod 16
If TotalweightOzExtra = "1" Or TotalweightOzExtra = "2" Or TotalweightOzExtra = "3" Or TotalweightOzExtra = "4" Or TotalweightOzExtra = "5" Or TotalweightOzExtra = "6" Or TotalweightOzExtra = "7" Or TotalweightOzExtra = "8" Or TotalweightOzExtra = "9" Then
TotalWeightLbs = TotalWeightLbs & ".0" & TotalweightOzExtra
Exit For
End If
If TotalweightOzExtra = "15" Or TotalweightOzExtra = "14" Or TotalweightOzExtra = "13" Or TotalweightOzExtra = "12" Or TotalweightOzExtra = "11" Or TotalweightOzExtra = "10" Then
TotalWeightLbs = CInt(TotalWeightLbs) & "." & TotalweightOzExtra
Exit For
End If
End If
End If
Next

russell
01-17-2007, 02:50 PM
i too like to write in vb so i can step thru and quickly debug. i do it a lot. i think u could use less code than u did, but heck, if it works, it works :)