adding numbers strings and negatives

I have a puzzling situation.

when i add strings such as

Dim Total As String
Dim subTotal As String

Total = Val(Total) + Val(Subtotal(

it trucates off the penny values or changes them to “.00”
however it does add negative number strings such as “-27.00”

but

when i try the same operations using Integer
ex:

Dim Total As Integer
Dim subTotal As Integer

Total = Total + Subtotal

it adds and keeps the penny values but does not add negative numbers,
it treats negatives as positives and adds them together.

Why is this or is there something i am doing wrong ?

Try this:

Dim Total As String
Dim subTotal As String
Dim nTotal as Double

nTotal = Val(Total) + Val(Subtotal)
Total = str(nTotal)

Check your results again. Are you sure you didn’t mix those 2 up? Integers will not retain decimals, ever. Also, your first code sample should throw a compile error. So, yeah, you’re doing something wrong, but i just don’t know what.

Based on what you say is happening, the code you have put here is not the code you are actually using.
Can you post your actual code?

you both are right i am doing somethig wrong.
i don’t mean to bore you with a lota code but i’d better be detailed to eliminate confusion and get this issue pinned down.

here is the code.

[code]Dim Clip As New Clipboard
Dim ClipSaveFile As String = “Inv_” + ReplaceAll(CusAddress.Text, " ", “_”) + “.pdf”
Clip.SetText ClipSaveFile

Dim HTMLinvoice As String
Dim row, col, ct As Integer
Dim wdate, Service, Units As String
Dim RtnArr(30) As String
Dim LeafLabor, Grass, Tree, Brush, Branch, RemovalFee, Hours As String
Dim cashamt, rtn, rtnval As String

Dim DiscountAmt As Integer
Dim discountTotalAllowed As Integer
Dim Amount As Integer
Dim discountMultiplier As String
Dim subtotal As Integer = 0
Dim Total As Integer

Dim displaysubtotal As String
Dim BillingAddress, PleaseFind As String
Dim detStr, PaidAmt_Credit As String
Dim InCash As String
Dim TOS As String
Dim TotalOther As Integer = 0

Dim hRate As String
Dim PreService As String
Dim StaticRate, Threshold, ThresholdIncrement, OverThreshold As String
Dim SnowArr(20) As String
Dim NL As String = Chr(13)
Dim BRK As String = “

Dim InvoiceType As String
Dim OverBalanceWarning As String
Dim OverBalanceMinPayment As String
Dim SendInvoiceByEmail As String

InvoiceType = Str(1)

if InvoiceID.Text = “” Then
MsgBox “No Invoice Created or Selected”
Return
End if

For ct = 0 To JobDetails.ListCount - 1
if CmpPattern(JobDetails.Cell(ct, 1), “Fee|Equipment|Transportation|Pre Bal|Previous Balance|Payment|Materials|Salt|Waist|Waste|Parts|Equipment|Rental|Equ Rent|Rental Fee|Discount”) = True or JobDetails.Cell(ct, 1) = “” then
JobDetails.Cell(ct, 4) = “-”
End If

Next ct

HTMLinvoice = MkHTMLInvoiceHeader(" - Landscape/Design - I n v o i c e", True) + “” + _
“” + NL

For row = 0 To JobDetails.ListCount - 1

wdate = JobDetails.Cell(row, 0)
Service = JobDetails.Cell(row, 1)
Units = JobDetails.Cell(row, 2)
Amount = val(JobDetails.Cell(row, 3))
TOS = JobDetails.Cell(row, 4)

If Amount > 0 or Amount < 0 Then
subtotal = Amount
Else
subtotal = 0
End If

If TOS = “-” Then
TotalOther = TotalOther + Amount
Else
discountTotalAllowed = discountTotalAllowed + subtotal
End If

displaysubtotal = Format(subtotal, “$0.00”)

If CmpPattern(Service, “Snow”) = False Then
if CmpPattern(Units, “%”) = true Then
elseif CmpPattern(Units, “ck|chk”) = True Then
else
Units = “”
end if
End If

’ blank entry

If wdate = “” And Service = “” Then
subtotal = 0
displaysubtotal = “”
End If

Total = Total + subtotal

HTMLinvoice = HTMLinvoice + “

” + NL
Next row

////////////////////

OverBalanceWarning = “

The amount shown is due 30 days from the date of this invoice

If Total > Val(CreditLimit.Text) or Total = Val(CreditLimit.Text) Then
OverBalanceWarning = “
Alert: Your Balance is past due. To avoid interruption in services and/or late fees please pay the amount indicated
The amount shown is due 30 days from the date of this invoice

End If

If BillEmail.Text <> “” Then
SendInvoiceByEmail = “

Our Records Show You Have an Email On File,
If you wish to recieve your future invoices by email please check this box [ ]
and return a copy of this printed invoice along with your payment

Else
SendInvoiceByEmail = “”
End If

HTMLinvoice = HTMLinvoice + “

” + NL
HTMLinvoice = HTMLinvoice + "” + NL
HTMLinvoice = HTMLinvoice + “
DATE SERVICE UNITS AMT TOS
” + wdate + “ ” + Service + “ ” + Units + “ ” + displaysubtotal + “ ” + TOS + “
===============================================================
Total Charge: $" + Format(Total, “0.00”) + " " + InCash + “
===============================================================
” + NL + _

HTMLinvoice = HTMLinvoice + OverBalanceWarning

If Discount.Text <> “” And Val(Discount.Text) <> 0 and Total > 0 and discountTotalAllowed > 0 Then
If InStr(Discount.Text, “30%”) > 0 Then
discountMultiplier = “0.7”
ElseIf InStr(Discount.Text, “25%”) > 0 Then
discountMultiplier = “0.75”
ElseIf InStr(Discount.Text, “20%”) > 0 Then
discountMultiplier = “0.8”
ElseIf InStr(Discount.Text, “20%”) > 0 Then
discountMultiplier = “0.85”
ElseIf InStr(Discount.Text, “10%”) > 0 Then
discountMultiplier = “0.9”
End If

DiscountAmt = (discountTotalAllowed * Val(discountMultiplier)) + TotalOther

//MsgBox "Discount Price: " + Str(DiscountAmt) + " Reg Price: " + Str(Total)

HTMLinvoice = HTMLinvoice + “

If paying this invoice in full in cash within 30 days of the invoice date, your discount offer will be: " + Discount.Text + " for a balance of: $” + Format(DiscountAmt, “$0.00”) + BRK + _
BRK + “Please Note: Discounts are not applied to Materials, Parts, Transportation, Equipment Fees, Disposal, Rentals, or Delivery Fees

End If

HTMLinvoice = HTMLinvoice + BRK + “

We accept Visa, M/C, Discover and American Express by Phone M-F 6pm - 8pm,” + BRK + _
“There is a %4 additional charge for credit card payments. which would bring your total to: $” + Format((Total * 0.04) + Total, “$0.00”) + _
BRK + BRK + “If paying by check please make payable to ‘Cullins Service’ for the amount of: $” + Format(Total, “$0.00”) + NL + BRK + “and mail to Cullins Service PO BOX 620-003 Newton Lower Falls, MA 02462

” + _
BRK + “( please make check ledgible, do not bend or fold check, do not write over the account information )” + _
SendInvoiceByEmail

HTMLinvoice = HTMLinvoice + BRK + BRK + "Thank You For Your Business

"

SaveToFile(HTMLinvoice, GetSettingsFolder.Child(“HTMLinvoice.html”))

GoToURLPath(GetSettingsPath + “/HTMLinvoice.html”)

// mark the current invoice as billed if flag has not been previously set
if ShowBillFlag = “” and DontMarkBilledChk.State = CheckBox.CheckedStates.Unchecked then
MarkAsBilled
end if
[/code]

Oh integers don’t retain decimals,
is there a floating point type of data type in XoJo?

[quote=333734:@David Cullins]Oh integers don’t retain decimals,
is there a floating point type of data type in XoJo?[/quote]
DOUBLE
SINGLE
CGFLOAT

Single and CGFloat are somewhat special-purpose. Double is the normal floating point data type.

so fellas should i be using Double if i want to retain the decimals in my routine ?

Yes

For any kind of accounting application, it would be better to use Currency.

Tim please explain what is Currency?

( 5 minutes later )
Actually i got it Tim, Currency is a data type.

Will Currency add -negative numbers, retain the decimals even if converted to a string …
there are 2 issues , one is the addition subtraction, the other is displaying the results in a app, or writing the results to a file.
Will Currency do all this ?

Currency is one of the built-in Xojo data types:

http://developer.xojo.com/currency

yep Paul, a finally got it.
thx

Yes.

Yes. Provided you do the conversion correctly. It’s not difficult, but it is possible to do it wrong.

It is generally a poor approach to store numeric data as a string. If you have no choice, it can be done, but it’s best to store your data in a database. It’s very easy to do.

For display, use Format(). If you must store it as a string, use Str() and Val().

Tim for clarification and edification, can you provide one example of the conversion done correctly, and one done incorrectly?
please thank you…

i use convert to double from string works perfect

[quote=333898:@David Cullins]Tim for clarification and edification, can you provide one example of the conversion done correctly, and one done incorrectly?
please thank you…[/quote]

The easiest way to get it “wrong” if you have to store it in a string is to use FORMAT especially if you have a multi user database that supports people in different countries with different formatting conventions.
Why ?
Format uses your LOCAL settings for how to make the string
So if you’re in one of the countries that uses the , for thousand separators and . for decimals then you end up with “123,456.010”
But I would say do not use thousands separators
Now the problem
IF someone in a different location that uses . for thousands and , for decimals tries to use CDBL or VAL to convert “123456.010” back to a number they will get the wrong number.
Why ? Because the . means “thousands” for this user NOT decimals as you intended

Instead if you use STR which ALWAYS users , for thousands and . for decimals and VAL, which ALWAYS interprets , as thousands and . as decimals you can the display it to the user in their preferred form

But store it in a consistent one

Another way to do it wrong is to use an integer instead of a double, as you did initially. To do it right is simple.

dim d as double 
dim s as string 
d = 1234.56
s = str(d)
file.writeline(s)
dim d as double
dim s as string
s = file.readline()
d = val(s)

Thank you Tim…