Checking if the number is decimal

kpark91

Well-known Member
Joined
Jul 15, 2010
Messages
1,582
Hello, I come with another programming question D:

I've been trying to solve Problem 3 of Project Euler with VBA and because I'm still a learner in every programming aspects. If either of these two questions are answered, it would mean ALOT to me :)

In VBA or any programming languages, when we're using Mod, the numbers have to be low enough to actually perform the function. (and they also need to be integers).

However, my number is too high and it cannot be converted to integers because of overflow. (1) So, is there such a function in VBA that allows me to find the remainder between the two doubles?

As far as I'm concerned, VBA does not have it or any other programming languages. So, this leads to my second question.

(2) How do I check if a double is a decimal number?


Thank you in advance!
kpark
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Maybe not the most elegant way, but can you convert it to a string and see if it contains a decimal point?
 
Upvote 0
Code:
IsWholeNumber = Int(myDbl) = myDbl
 
Upvote 0
Not really sure about this, but a chance to learn if I've blown it...

Rich (BB code):
Sub exa4()
Dim dblVal1 As Double, dblVal2 As Double
Dim dblRemainder As Double
    
dblVal1 = 97003.5435
dblVal2 = 89778.0003
    
MsgBox "Double has val after decimal: " & Not CBool(dblVal1 - CLng(dblVal1) = 0)
    
dblRemainder = ((dblVal1 / dblVal2) - CLng((dblVal1 / dblVal2))) * dblVal2
End Sub
 
Upvote 0
djreiswig
oh, yeahh I totally forgot about "." in strings haha. yeah. That would be a good idea :P and I've tried it and it worked!!

shg
Wha... Why did it work lol
I tried it before and it didn't work -_-
I made it as a function where it was this:
Rich (BB code):
Function IsWholeNumber(myDbl As Double)
    IsWholeNumber = Int(myDbl) = myDbl
End Function

GTO
Hello, your decimal thingy works but not the dblRemainder because of overflow.
I am thinking it is because of
Rich (BB code):
CLng(dblVal1 / dblVal2)
The value I am working with is 600851475143 and... it's way over 2 million, which is Long variable's limit.
However, it was working with smaller numbers and I think I can get the remainder after looking at yours.


Thank you very much to all of you for helping me out :)
They were all different ideas and they were all amazing!

Thanks again,
kpark
 
Upvote 0
Int doesn't convert a number to Integer (that's CInt).

You could use this for the MOD function:
Code:
Function myMod(d1 As Double, d2 As Double) As Double
    ' Returns MOD(d1, d2)
    myMod = d1 - Int(d1 / d2) * d2
End Function
 
Upvote 0
GTO:

Your use of CBool is redundant:

This is already a Boolean Expression:

dblVal1 - CLng(dblVal1) = 0
 
Upvote 0
shg
Oh... lol. I have made the problem more complicated on my own...
Gahh this is embarassing xD
Thank you very much!
I was working on this problem for like an hour now...
 
Upvote 0
Hi shg,

For whatever reason, I have always assumed Int() could run into overflow (probably the name), but I see that this is incorrect. Am I correct in presuming it simply truncates without limitation to size of number?

Thanks,

Mark
 
Upvote 0
Oops. Thank you HotPepper, I had not seen your post.
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,738
Members
452,940
Latest member
Lawrenceiow

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top