CInt Question

1. Senior Member
Join Date
Dec 1969
Posts
179

## CInt Question

Could someone tell me why this code displays 10.75 not 1.75?<BR><BR>str = "1 3/4"<BR>iWhole = CInt(split(str, " ")(0))<BR>StrFraction = split(str, " ")(1)<BR>strFraction = Eval(strFraction)<BR>strDecimal = iWhole & strFraction<BR>response.Write strDecimal<BR><BR>Thanks

2. Senior Member
Join Date
Dec 1969
Posts
196

## RE: CInt Question

str = "1 3/4"<BR>iWhole = CInt(split(str, " ")(0))<BR>StrFraction = split(str, " ")(1)<BR>strFraction = Eval(strFraction)<BR>Response.Write(iWhole & "<BR>")<BR>Response.Write(strFraction & "<BR>")<BR><BR>See the problem now?<BR><BR>And the "fix":<BR>dblDecimal = iWhole + cDbl(strFraction)<BR>Response.Write(dblDecimal)

3. Senior Member
Join Date
Dec 1969
Posts
196

## RE: CInt Question

An easier way perhaps:<BR><BR>Haven&#039;t tested it, but it *should* work :)<BR><BR>str = "1 3/4"<BR>strFunction = Replace(str," ","+(") & ")"<BR>dblDeciaml = eval(strFunction)

4. Senior Member
Join Date
Dec 1969
Posts
179

## Yes!

Thanks a lot!

5. Senior Member
Join Date
Dec 1969
Posts
179

## Another Question

If i remove the 1 from 1 3/4 the script fails.<BR>Do i need to include a if/then?

6. Senior Member
Join Date
Dec 1969
Posts
196

## RE: Another Question

Oh, that&#039;s right.<BR>Better split it up then. Like your first example.<BR><BR>This should work if str="1", str="3/4", str="1 3/4", etc...<BR><BR>str = "1 3/4"<BR>parts = split(str," ")<BR>intPart = 0<BR>strPart = 0<BR>If InStr(parts(0),"/") &#062; 0 Then<BR> strPart = parts(0)<BR>Else<BR> intPart = parts(0)<BR> If UBound(parts) &#062; 0 Then strPart = parts(1)<BR>End If<BR><BR>dblDeciaml = intPart + eval(strPart)

7. Senior Member
Join Date
Dec 1969
Posts
179

## Cool

If I wanted to prevent the failure of the script due to extra spaces in the string could I use a Trim() first to delete the spaces like str = "1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3/4"<BR><BR>value = "5 3/4"<BR>str = Trim(value)<BR>parts = split(str," ")<BR>intPart = 0<BR>strPart = 0<BR>If InStr(parts(0),"/") &#062; 0 Then<BR>strPart = parts(0)<BR>Else<BR>intPart = parts(0)<BR>If UBound(parts) &#062; 0 Then strPart = parts(1)<BR>End If<BR><BR>dblDeciaml = intPart + eval(strPart)<BR>

8. Senior Member
Join Date
Dec 1969
Posts
196

## RE: Cool

Trim won&#039;t work, since it only takes away spaces in front of and behind the text. But you could do something like this (probably BAD code):<BR><BR>While InStr(str," ") &#060;&#062; 0<BR> str = Replace(str," "," ")<BR>Wend

9. Senior Member
Join Date
Dec 1969
Posts
179

## Thank You!

Excellent! After posting the last message I played with the trim function and figured out that it would not work, I was trying the replace function with some success.<BR>Now it&#039;s complete!<BR>Thank You Again!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•