Converting a number to an Integer or a Double based on number after the decimal

I have a program that populates data onto an excel sheet.  I have the data in my SQL 2005 DB as a Numeric(8,2) datatype.

When I pull the data to populate the excel sheet, I want to either insert the number with a decimal or no decimal depending on the value.

So if the value is "4.00" I want to insert "4" to the excel sheet.  If the value is "4.25" I want to insert "4.25" to the excel sheet.

How do i do that?
Who is Participating?
Christopher KileConnect With a Mentor Commented:
Do you want to simply display this, or post an integer to the sheet?  Because, by default, Excel displays 4.0 as 4.
Otherwise, if you are willing to send strings to Excel,
Dim realVal as Double
Dim outputString as String
If realVal = System.Math.Floor(realVal) Then
    outputString = Cint(realVal).ToString
    outputString = realVal.ToString
End If

If you insert it as a number then Excel will always store it as decimal number behind the scenes.  You could store it as a string but I suspect that would not be the right option since Excel arithmentic would not work.

Which leaves the column formatting in the excel sheet.  If you use Excel General formatting then you should get the results you want.  If you use Number that you will always get the number of decimal places specified on the format.

Hope this helps
If you want to inser

Not much of a solution this
logoncomAuthor Commented:
works, thanks
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.