Wrox Programmer Forums How to check if a number has a decimal
 | Search | Today's Posts | Mark Forums Read
 VB How-To Ask your "How do I do this with VB?" questions in this forum.
 Welcome to the p2p.wrox.com Forums. You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com

August 5th, 2004, 09:03 AM
 Authorized User Join Date: Jan 2004 Location: , , . Posts: 34 Thanks: 0 Thanked 1 Time in 1 Post
How to check if a number has a decimal

How can I check to if a number has a decimal or not?

August 5th, 2004, 09:06 AM
 Friend of Wrox Join Date: Aug 2003 Location: , , . Posts: 205 Thanks: 0 Thanked 0 Times in 0 Posts

If Instr(1,Number,".") > 0 Then

Yehuda

August 5th, 2004, 12:27 PM
 Friend of Wrox Join Date: Jun 2003 Location: Alameda, ca, USA. Posts: 627 Thanks: 0 Thanked 0 Times in 0 Posts

Yehuda, that works only in English (or any other language that use the dot as separator.)
A safer (and faster) test:

if Fix(lNum) <> lNum then

Marco

August 5th, 2004, 12:52 PM
 Friend of Wrox Join Date: Jun 2003 Location: McPherson, Kansas, USA. Posts: 128 Thanks: 0 Thanked 0 Times in 0 Posts

Interesting. I am curious as to your reasoning but this should work.

Dim fVal As Single
Dim lVal As Long
Dim bIsWholeNumber As Boolean

' This is your number: input
fVal = 1956854.5684 ' Your number
' Convert to a long
lVal = CLng(fVal)
' Test to see if the values are equal
' If no then you know the original value
' was not a whole number
bIsWholeNumber = IIf(lVal = fVal, True, False)

' Do what ever you do after you know
MsgBox bIsWholeNumber

Let me know if this works for you.

Larry Asher

August 5th, 2004, 02:13 PM
 Friend of Wrox Join Date: Aug 2003 Location: , , . Posts: 205 Thanks: 0 Thanked 0 Times in 0 Posts

I had forgotten about the country issue (it has been quite a while since I have developed a multinational product). Thanks.

August 5th, 2004, 02:38 PM
 Friend of Wrox Join Date: Jun 2003 Location: Alameda, ca, USA. Posts: 627 Thanks: 0 Thanked 0 Times in 0 Posts

Larry, your code will work only the if it is possible to convert the number into a long. With big numbers the statement CLng(fVal) will thrown an error (this is why I used Fix instead of CLng)

BTW the line
bIsWholeNumber = IIf(lVal = fVal, True, False)
could be written simply (and faster) as
bIsWholeNumber = (lVal = fVal)
(well, I hate the IIF... it is so moronic)

Marco

 Similar Threads Thread Thread Starter Forum Replies Last Post Number with 2 decimal places only pallone SQL Server 2000 4 July 3rd, 2007 07:29 AM function to create whole number from decimal dhx10000 Classic ASP Professional 1 July 18th, 2006 08:17 PM round up a decimal number semooth VB How-To 3 October 4th, 2005 02:33 PM Decimal check Nitin_sharma Javascript 4 November 25th, 2004 12:03 PM number of decimal? hosefo81 PHP How-To 1 February 11th, 2004 02:25 AM