I was hoping there is a way to avoid an if statement or doing string manipulation, by changing the format string. I’m using this instruction in an loop that iterates 1 million times, so I need to limit the amount of instructions used in the loop.
Hmmm, I just remembered Michael Diehr recommended a fast check for integers in the “Is whole number” conversation…
dim temp as string
// dval = some double value
if (dVal \\ 1) = dVal then // whole number?
msgbox Str(dVal, "-0")
else
msgbox Str(dVal, "-0.#######")
end if
This is the method I finally settled on to convert a double to string, while preventing automatic scientific notation, and dropping the decimal symbol if it is a whole number:
Private Function X3_Str(d As Double) As String
if (d \\ 1) = d then // whole number?
return Str(d, "-0")
else
return Str(d, "-0.#######")
end if
End Function
Here is a method that I use for detecting integer values:
Public Function IsInteger(d As Double) as Boolean
//returns true if the argument is an integer value to 10 or more decimal places
return d-floor(abs(d+5e-11))<1e-10
End Function
…and 3 more years later. I have a specific need for an oddly formatted Excel sheet I have to read in. Alwyn, in the short function, you have:
if (d \\ 1) = d then // whole number?
Is that supposed to be integer division with just one slash like this? :
if (d \ 1) = d then
The code throws an error with \ used. Seems to work as I would expect with the normal integer division. Or am I missing something that \ is supposed to do? Maybe it didn’t get translated correctly in the move to the new forum?
Thanks Alwyn. It is in Xojo code, the reader is actually LibXL in the Monkeybread libraries, which reads in the cell as a number or a string and then I have to parse it. This particular Excel file has really big integer values that were getting translated as scientific notation. The function you posted above actually works really well, I’ve been testing it today. Thanks again, so nice to be able to get help here when needed. And thanks for the response Emile, I haven’t been on the forums much lately too busy with work, but checking in occasionally.