Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Checking if a number is Decimal or integer


Reply
 
Thread Tools Display Modes
  #1  
Old 01-15-2005, 04:21 PM
mathewmunro mathewmunro is offline
Regular
 
Join Date: Nov 2003
Posts: 71
Default Checking if a number is Decimal or integer


How do i know if a number is a decimal ,or a Number with no decimals points? is there a function built into Vb ??
Reply With Quote
  #2  
Old 01-15-2005, 04:25 PM
SteRobson's Avatar
SteRobson SteRobson is offline
Contributor
 
Join Date: Apr 2003
Location: York, England
Posts: 475
Default

Quote:
Originally Posted by mathewmunro
How do i know if a number is a decimal ,or a Number with no decimals points? is there a function built into Vb ??
You could perhaps use the Round function to round your number down to zero decimal places. Then, if the number's value hasn't changed then it was an integer, otherwise it must have been a decimal.
__________________
S t e

On Error Goto Blame_Someone_Else
Reply With Quote
  #3  
Old 01-15-2005, 04:33 PM
stevo stevo is offline
Senior Contributor

* Expert *
 
Join Date: May 2004
Location: Manchester, England.
Posts: 1,254
Default

there is no built in function but you can use Int().
Code:
Dim num1 As Double num1 = 23.34 If num1 = Int(num1) Then MsgBox "As no decimal places" Else MsgBox "As decimal places" End If
Reply With Quote
  #4  
Old 01-15-2005, 05:06 PM
webbone's Avatar
webbone webbone is offline
Hydrogen Powered

Administrator
* Expert *
 
Join Date: Jul 2003
Location: Sacramento, CA
Posts: 6,090
Default

What sort of numbers are you trying to check? If you are only doing positive numbers then steveo's example will work. If you need to check negative numbers you will need to use the Fix() function instead of Int(). See the difference on MSDN: http://msdn.microsoft.com/library/de...l/vafctint.asp
__________________
"With the appearance of the AddressOf operator, an entire industry has developed among authors illustrating how to do previously impossible tasks using Visual Basic. Another industry is rapidly developing among consultants helping users who have gotten into trouble attempting these tasks." -Dan Appleman
Reply With Quote
  #5  
Old 01-15-2005, 06:16 PM
unclebill unclebill is offline
Junior Contributor
 
Join Date: Apr 2003
Location: San Diego, CA
Posts: 305
Default

Quote:
Originally Posted by stevo
there is no built in function but you can use Int().
Code:
Dim num1 As Double num1 = 23.34 If num1 = Int(num1) Then MsgBox "As no decimal places" Else MsgBox "As decimal places" End If
I thought that's what VarType() does.
Reply With Quote
  #6  
Old 01-15-2005, 06:33 PM
stevo stevo is offline
Senior Contributor

* Expert *
 
Join Date: May 2004
Location: Manchester, England.
Posts: 1,254
Default

thats correct if you can put the number straight into the function like
Code:
If VarType(23.34) = vbInteger Then 'returns false
but i doubt if the number will be going straight into the function so if you did this
Code:
Dim num As Double If VarType(num) = vbInteger Then
that will return false even if the number is an integer.
Reply With Quote
  #7  
Old 01-16-2005, 01:50 PM
unclebill unclebill is offline
Junior Contributor
 
Join Date: Apr 2003
Location: San Diego, CA
Posts: 305
Default

I misinterpreted the question. At first, I thought the question was, 'is the variable an integer or decomal' type. The real question seems to be, 'is the value of the variable an integer or a number with a fractional component'.
Reply With Quote
  #8  
Old 01-16-2005, 03:11 PM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,911
Default

Quote:
Originally Posted by webbone
What sort of numbers are you trying to check? If you are only doing positive numbers then steveo's example will work. If you need to check negative numbers you will need to use the Fix() function instead of Int(). See the difference on MSDN: http://msdn.microsoft.com/library/de...l/vafctint.asp
While it is good to know the difference between fix and int functions, in this
case, steveo's example will work regardless of whether you use the Fix or Int
function since the purpose is to determine whether the number has a fractional part, not what the integer part is.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #9  
Old 01-16-2005, 03:13 PM
webbone's Avatar
webbone webbone is offline
Hydrogen Powered

Administrator
* Expert *
 
Join Date: Jul 2003
Location: Sacramento, CA
Posts: 6,090
Default

Quote:
Originally Posted by passel
While it is good to know the difference between fix and int functions, in this
case, steveo's example will work regardless of whether you use the Fix or Int
function since the purpose is to determine whether the number has a fractional part, not what the interger part is.
Oops - you are very right about that! I wasn't thinking through what the actual goal of the statement was! Thanks for pointing this out for us all!
__________________
"With the appearance of the AddressOf operator, an entire industry has developed among authors illustrating how to do previously impossible tasks using Visual Basic. Another industry is rapidly developing among consultants helping users who have gotten into trouble attempting these tasks." -Dan Appleman
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->