#### how to check if a number has no more than 2 decimal digits

```I need to do some input validation so to check if a value has no more
than 2 decimal digits.  So 14.12 is valid but 14.123 is not.

I have tried doing a check like this:

If Int(inval * 100) <> inval * 100 Then

but this gets a rounding error with certain values like 2.22.  If I
subtract one side above from the other I get a difference on the order
of 10^-21.

So I tried rounding the numbers to do a test like this:

If Round(Int(inval * 100), 10) <> Round(inval * 100, 10) Then

and this does something weird like rounding Round(Int(0.29 * 100), 10)
to 28.

Is there some simpler way to check that a number does not have too
many decimal digits?
```
 0
zxcv
3/24/2010 3:08:30 PM
excel.programming 6508 articles. 2 followers.

12 Replies
1650 Views

Similar Articles

[PageSpeed] 22

```This worked pretty good.  You can adapt it to your needs.

Sub dk()

For Each c In Range("A2:A6")
If Len(c) - InStr(c, ".") > 2 Then
MsgBox c.Address & "  More than 2 decimal places"
End If
Next
End Sub

"zxcv" <zxcvnosend@yahoo.com> wrote in message
news:3466ddcc-9050-4873-9377-bcaee5b6517e@z3g2000yqz.googlegroups.com...
>I need to do some input validation so to check if a value has no more
> than 2 decimal digits.  So 14.12 is valid but 14.123 is not.
>
> I have tried doing a check like this:
>
>     If Int(inval * 100) <> inval * 100 Then
>
> but this gets a rounding error with certain values like 2.22.  If I
> subtract one side above from the other I get a difference on the order
> of 10^-21.
>
> So I tried rounding the numbers to do a test like this:
>
>     If Round(Int(inval * 100), 10) <> Round(inval * 100, 10) Then
>
> and this does something weird like rounding Round(Int(0.29 * 100), 10)
> to 28.
>
> Is there some simpler way to check that a number does not have too
> many decimal digits?

```
 0
JLGWhiz
3/24/2010 3:27:43 PM
```Your code will fail if the number is a whole number without a decimal point.
If you change your If..Then statement to the following, then your code will
work correctly...

If Len(c) - InStr(c & ".", ".") > 2 Then

--
Rick (MVP - Excel)

"JLGWhiz" <JLGWhiz@cfl.rr.com> wrote in message
news:#sweMa2yKHA.4752@TK2MSFTNGP04.phx.gbl...
> This worked pretty good.  You can adapt it to your needs.
>
> Sub dk()
>
>  For Each c In Range("A2:A6")
>    If Len(c) - InStr(c, ".") > 2 Then
>       MsgBox c.Address & "  More than 2 decimal places"
>    End If
>  Next
> End Sub
>
>
>
> "zxcv" <zxcvnosend@yahoo.com> wrote in message
> news:3466ddcc-9050-4873-9377-bcaee5b6517e@z3g2000yqz.googlegroups.com...
>>I need to do some input validation so to check if a value has no more
>> than 2 decimal digits.  So 14.12 is valid but 14.123 is not.
>>
>> I have tried doing a check like this:
>>
>>     If Int(inval * 100) <> inval * 100 Then
>>
>> but this gets a rounding error with certain values like 2.22.  If I
>> subtract one side above from the other I get a difference on the order
>> of 10^-21.
>>
>> So I tried rounding the numbers to do a test like this:
>>
>>     If Round(Int(inval * 100), 10) <> Round(inval * 100, 10) Then
>>
>> and this does something weird like rounding Round(Int(0.29 * 100), 10)
>> to 28.
>>
>> Is there some simpler way to check that a number does not have too
>> many decimal digits?
>
>
```
 0
Rick
3/24/2010 4:04:59 PM
```"zxcv" <zxcvnosend@yahoo.com> wrote:
> I need to do some input validation so to check if a
> value has no more than 2 decimal digits.  So 14.12
> is valid but 14.123 is not.

Try:

If Round(inval,2) = inval Then

Normally, I would opt for WorksheetFunction.Round or even
Evaluate("round(...)") instead of the VB Round function.  There are
functional differences.  In this case, I do not think it makes a difference.
Nevertheless, you might want to use one of those alternatives instead, just
to be sure.

----- original message -----

"zxcv" <zxcvnosend@yahoo.com> wrote in message
news:3466ddcc-9050-4873-9377-bcaee5b6517e@z3g2000yqz.googlegroups.com...
>I need to do some input validation so to check if a value has no more
> than 2 decimal digits.  So 14.12 is valid but 14.123 is not.
>
> I have tried doing a check like this:
>
>     If Int(inval * 100) <> inval * 100 Then
>
> but this gets a rounding error with certain values like 2.22.  If I
> subtract one side above from the other I get a difference on the order
> of 10^-21.
>
> So I tried rounding the numbers to do a test like this:
>
>     If Round(Int(inval * 100), 10) <> Round(inval * 100, 10) Then
>
> and this does something weird like rounding Round(Int(0.29 * 100), 10)
> to 28.
>
> Is there some simpler way to check that a number does not have too
> many decimal digits?

```
 0
Joe
3/24/2010 4:31:20 PM
```Hi Rick, I ran a test and it did not error out.  But it does not hurt to be
safe.

"Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message
news:OZHMFv2yKHA.4752@TK2MSFTNGP04.phx.gbl...
> Your code will fail if the number is a whole number without a decimal
> point. If you change your If..Then statement to the following, then your
> code will work correctly...
>
> If Len(c) - InStr(c & ".", ".") > 2 Then
>
> --
> Rick (MVP - Excel)
>
>
>
> "JLGWhiz" <JLGWhiz@cfl.rr.com> wrote in message
> news:#sweMa2yKHA.4752@TK2MSFTNGP04.phx.gbl...
>> This worked pretty good.  You can adapt it to your needs.
>>
>> Sub dk()
>>
>>  For Each c In Range("A2:A6")
>>    If Len(c) - InStr(c, ".") > 2 Then
>>       MsgBox c.Address & "  More than 2 decimal places"
>>    End If
>>  Next
>> End Sub
>>
>>
>>
>> "zxcv" <zxcvnosend@yahoo.com> wrote in message
>> news:3466ddcc-9050-4873-9377-bcaee5b6517e@z3g2000yqz.googlegroups.com...
>>>I need to do some input validation so to check if a value has no more
>>> than 2 decimal digits.  So 14.12 is valid but 14.123 is not.
>>>
>>> I have tried doing a check like this:
>>>
>>>     If Int(inval * 100) <> inval * 100 Then
>>>
>>> but this gets a rounding error with certain values like 2.22.  If I
>>> subtract one side above from the other I get a difference on the order
>>> of 10^-21.
>>>
>>> So I tried rounding the numbers to do a test like this:
>>>
>>>     If Round(Int(inval * 100), 10) <> Round(inval * 100, 10) Then
>>>
>>> and this does something weird like rounding Round(Int(0.29 * 100), 10)
>>> to 28.
>>>
>>> Is there some simpler way to check that a number does not have too
>>> many decimal digits?
>>
>>

```
 0
JLGWhiz
3/24/2010 4:58:53 PM
```By fail I meant it will return the wrong result, not error out. I assumed
from the OP's posting that whole numbers as well as floating point numbers
with one or two decimal places were okay... your original If...Then
statement reported one and two digits after the decimal point as being okay,
but listed whole numbers as having more than two decimal places (if the
whole number had more than two digits in it).

--
Rick (MVP - Excel)

"JLGWhiz" <JLGWhiz@cfl.rr.com> wrote in message
news:u5\$pIN3yKHA.1796@TK2MSFTNGP02.phx.gbl...
> Hi Rick, I ran a test and it did not error out.  But it does not hurt to
> be safe.
>
>
> "Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message
> news:OZHMFv2yKHA.4752@TK2MSFTNGP04.phx.gbl...
>> Your code will fail if the number is a whole number without a decimal
>> point. If you change your If..Then statement to the following, then your
>> code will work correctly...
>>
>> If Len(c) - InStr(c & ".", ".") > 2 Then
>>
>> --
>> Rick (MVP - Excel)
>>
>>
>>
>> "JLGWhiz" <JLGWhiz@cfl.rr.com> wrote in message
>> news:#sweMa2yKHA.4752@TK2MSFTNGP04.phx.gbl...
>>> This worked pretty good.  You can adapt it to your needs.
>>>
>>> Sub dk()
>>>
>>>  For Each c In Range("A2:A6")
>>>    If Len(c) - InStr(c, ".") > 2 Then
>>>       MsgBox c.Address & "  More than 2 decimal places"
>>>    End If
>>>  Next
>>> End Sub
>>>
>>>
>>>
>>> "zxcv" <zxcvnosend@yahoo.com> wrote in message
>>> news:3466ddcc-9050-4873-9377-bcaee5b6517e@z3g2000yqz.googlegroups.com...
>>>>I need to do some input validation so to check if a value has no more
>>>> than 2 decimal digits.  So 14.12 is valid but 14.123 is not.
>>>>
>>>> I have tried doing a check like this:
>>>>
>>>>     If Int(inval * 100) <> inval * 100 Then
>>>>
>>>> but this gets a rounding error with certain values like 2.22.  If I
>>>> subtract one side above from the other I get a difference on the order
>>>> of 10^-21.
>>>>
>>>> So I tried rounding the numbers to do a test like this:
>>>>
>>>>     If Round(Int(inval * 100), 10) <> Round(inval * 100, 10) Then
>>>>
>>>> and this does something weird like rounding Round(Int(0.29 * 100), 10)
>>>> to 28.
>>>>
>>>> Is there some simpler way to check that a number does not have too
>>>> many decimal digits?
>>>
>>>
>
>
```
 0
Rick
3/24/2010 5:08:40 PM
```On Mar 24, 12:04=A0pm, "Rick Rothstein"
<rick.newsNO.S...@NO.SPAMverizon.net> wrote:
> Your code will fail if the number is a whole number without a decimal poi=
nt.
> If you change your If..Then statement to the following, then your code wi=
ll
> work correctly...
>
> If Len(c) - InStr(c & ".", ".") > 2 Then
>
> --
> Rick (MVP - Excel)
>
> "JLGWhiz" <JLGW...@cfl.rr.com> wrote in message
>
> news:#sweMa2yKHA.4752@TK2MSFTNGP04.phx.gbl...
>
> > This worked pretty good. =A0You can adapt it to your needs.
>
> > Sub dk()
>
> > =A0For Each c In Range("A2:A6")
> > =A0 =A0If Len(c) - InStr(c, ".") > 2 Then
> > =A0 =A0 =A0 MsgBox c.Address & " =A0More than 2 decimal places"
> > =A0 =A0End If
> > =A0Next
> > End Sub
>
> > "zxcv" <zxcvnos...@yahoo.com> wrote in message
> >news:3466ddcc-9050-4873-9377-bcaee5b6517e@z3g2000yqz.googlegroups.com...
> >>I need to do some input validation so to check if a value has no more
> >> than 2 decimal digits. =A0So 14.12 is valid but 14.123 is not.
>
> >> I have tried doing a check like this:
>
> >> =A0 =A0 If Int(inval * 100) <> inval * 100 Then
>
> >> but this gets a rounding error with certain values like 2.22. =A0If I
> >> subtract one side above from the other I get a difference on the order
> >> of 10^-21.
>
> >> So I tried rounding the numbers to do a test like this:
>
> >> =A0 =A0 If Round(Int(inval * 100), 10) <> Round(inval * 100, 10) Then
>
> >> and this does something weird like rounding Round(Int(0.29 * 100), 10)
> >> to 28.
>
> >> Is there some simpler way to check that a number does not have too
> >> many decimal digits?

Thanks.  A combination of the 2 above approaches is working.
```
 0
zxcv
3/24/2010 5:08:48 PM
```On Mar 24, 12:31=A0pm, "Joe User" <joeu2004> wrote:
> "zxcv" <zxcvnos...@yahoo.com> wrote:
> > I need to do some input validation so to check if a
> > value has no more than 2 decimal digits. =A0So 14.12
> > is valid but 14.123 is not.
>
> Try:
>
> If Round(inval,2) =3D inval Then
>
> Normally, I would opt for WorksheetFunction.Round or even
> Evaluate("round(...)") instead of the VB Round function. =A0There are
> functional differences. =A0In this case, I do not think it makes a differ=
ence.
> Nevertheless, you might want to use one of those alternatives instead, ju=
st
> to be sure.
>
> ----- original message -----
>
> "zxcv" <zxcvnos...@yahoo.com> wrote in message
>
> news:3466ddcc-9050-4873-9377-bcaee5b6517e@z3g2000yqz.googlegroups.com...
>
> >I need to do some input validation so to check if a value has no more
> > than 2 decimal digits. =A0So 14.12 is valid but 14.123 is not.
>
> > I have tried doing a check like this:
>
> > =A0 =A0 If Int(inval * 100) <> inval * 100 Then
>
> > but this gets a rounding error with certain values like 2.22. =A0If I
> > subtract one side above from the other I get a difference on the order
> > of 10^-21.
>
> > So I tried rounding the numbers to do a test like this:
>
> > =A0 =A0 If Round(Int(inval * 100), 10) <> Round(inval * 100, 10) Then
>
> > and this does something weird like rounding Round(Int(0.29 * 100), 10)
> > to 28.
>
> > Is there some simpler way to check that a number does not have too
> > many decimal digits?

Thanks for the input but I need to do this in VBA as I have no control
over the input and cannot put any formulas in the sheet.  Someone else
enters the data and then another person hits a button that I created.
```
 0
zxcv
3/24/2010 5:10:34 PM
```Embellishment....

"zxcv" <zxcvnosend@yahoo.com> wrote:
> I tried rounding the numbers to do a test like this:
> If Int(inval * 100) <> inval * 100 Then
>
> but this gets a rounding error with certain values like 2.22.

The reason that does not work is because most numbers with decimal fractions
cannot be represented exactly.  Instead, they are represented by a sum of 53
consecutive powers of two (bits), for example 2*2^1 + 0*2^0 + 0*2^-1 +
0*2^-2 + 1*2^-3 + etc.

Consequently, 2.22 is represented by exactly
2.22000000000000,0195399252334027551114559173583984375.  Int(2.22*100) is
exactly 222.  But 2.22*100 is
222.000000000000,028421709430404007434844970703125, preserving the
additional bits used to approximate 0.22 in this context.

In contrast, Round(inval,2) results in inval exactly as it would be
represented internally if it were entered with 2 decimal places.  So if
inval is 2.22, Round(inval,2) results in
2.22000000000000,0195399252334027551114559173583984375.  But if inval were
2.22+2^-51 (the smallest value larger than 2.22), it would be represented
internally as 2.22000000000000,06394884621840901672840118408203125, and
Round(inval,2) does not equal inval.

Note:  You cannot enter the
2.2200000000000006394884621840901672840118408203125 as a constant in Excel;
however, it can be the result of a calculation.  Also, you can enter that
constant in VBA, including as input to an InputBox.  Caveat:  If you write
that constant in a VBA statement, the VBA editor might change it later when
you edit the line.  It would be more reliable to write
Cdbl("2.2200000000000006394884621840901672840118408203125").

----- original message -----

"Joe User" <joeu2004> wrote in message
news:%233Lsx92yKHA.5036@TK2MSFTNGP02.phx.gbl...
> "zxcv" <zxcvnosend@yahoo.com> wrote:
>> I need to do some input validation so to check if a
>> value has no more than 2 decimal digits.  So 14.12
>> is valid but 14.123 is not.
>
> Try:
>
> If Round(inval,2) = inval Then
>
> Normally, I would opt for WorksheetFunction.Round or even
> Evaluate("round(...)") instead of the VB Round function.  There are
> functional differences.  In this case, I do not think it makes a
> difference. Nevertheless, you might want to use one of those alternatives
> instead, just to be sure.
>
>
> ----- original message -----
>
> "zxcv" <zxcvnosend@yahoo.com> wrote in message
> news:3466ddcc-9050-4873-9377-bcaee5b6517e@z3g2000yqz.googlegroups.com...
>>I need to do some input validation so to check if a value has no more
>> than 2 decimal digits.  So 14.12 is valid but 14.123 is not.
>>
>> I have tried doing a check like this:
>>
>>     If Int(inval * 100) <> inval * 100 Then
>>
>> but this gets a rounding error with certain values like 2.22.  If I
>> subtract one side above from the other I get a difference on the order
>> of 10^-21.
>>
>> So I tried rounding the numbers to do a test like this:
>>
>>     If Round(Int(inval * 100), 10) <> Round(inval * 100, 10) Then
>>
>> and this does something weird like rounding Round(Int(0.29 * 100), 10)
>> to 28.
>>
>> Is there some simpler way to check that a number does not have too
>> many decimal digits?
>

```
 0
Joe
3/24/2010 5:30:10 PM
```"zxcv" <zxcvnosend@yahoo.com> wrote:
On Mar 24, 12:31 pm, "Joe User" <joeu2004> wrote:
> > Try:
> > If Round(inval,2) = inval Then
[....]
> Thanks for the input but I need to do this in VBA
> as I have no control over the input and cannot put
> any formulas in the sheet.  Someone else enters the
> data and then another person hits a button that I
> created.

I don't understand your comment.  What I wrote is for VBA, and it is
intended to deal with exactly the situation that you describe.  I think you
misunderstand my comments.  Perhaps you should just give it a try.

PS:  Sorry, I wrote "=" where you wanted "<>".  That's a simple change, heh?

----- original message -----

"zxcv" <zxcvnosend@yahoo.com> wrote in message
news:6d74dc36-1fae-4269-8bba-dd366ae0b776@g28g2000yqh.googlegroups.com...
On Mar 24, 12:31 pm, "Joe User" <joeu2004> wrote:
> "zxcv" <zxcvnos...@yahoo.com> wrote:
> > I need to do some input validation so to check if a
> > value has no more than 2 decimal digits. So 14.12
> > is valid but 14.123 is not.
>
> Try:
>
> If Round(inval,2) = inval Then
>
> Normally, I would opt for WorksheetFunction.Round or even
> Evaluate("round(...)") instead of the VB Round function. There are
> functional differences. In this case, I do not think it makes a
> difference.
> Nevertheless, you might want to use one of those alternatives instead,
> just
> to be sure.
>
> ----- original message -----
>
> "zxcv" <zxcvnos...@yahoo.com> wrote in message
>
> news:3466ddcc-9050-4873-9377-bcaee5b6517e@z3g2000yqz.googlegroups.com...
>
> >I need to do some input validation so to check if a value has no more
> > than 2 decimal digits. So 14.12 is valid but 14.123 is not.
>
> > I have tried doing a check like this:
>
> > If Int(inval * 100) <> inval * 100 Then
>
> > but this gets a rounding error with certain values like 2.22. If I
> > subtract one side above from the other I get a difference on the order
> > of 10^-21.
>
> > So I tried rounding the numbers to do a test like this:
>
> > If Round(Int(inval * 100), 10) <> Round(inval * 100, 10) Then
>
> > and this does something weird like rounding Round(Int(0.29 * 100), 10)
> > to 28.
>
> > Is there some simpler way to check that a number does not have too
> > many decimal digits?

Thanks for the input but I need to do this in VBA as I have no control
over the input and cannot put any formulas in the sheet.  Someone else
enters the data and then another person hits a button that I created.

```
 0
Joe
3/24/2010 5:35:53 PM
```Yes, I did not account for the 3 digit whole number.  The modified code
below would also eliminate that possibility and restrict the items tested to
only those with decimal values.

Sub decDig()
For Each c In Range("A2:A5")
If InStr(c, ".") > 0 Then
If Len(c) - InStr(c, ".") > 2 Then
MsgBox c.Address & " OK"
End If
End If
Next
End Sub

"Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message
news:%23xBbqS3yKHA.6140@TK2MSFTNGP05.phx.gbl...
> By fail I meant it will return the wrong result, not error out. I assumed
> from the OP's posting that whole numbers as well as floating point numbers
> with one or two decimal places were okay... your original If...Then
> statement reported one and two digits after the decimal point as being
> okay, but listed whole numbers as having more than two decimal places (if
> the whole number had more than two digits in it).
>
> --
> Rick (MVP - Excel)
>
>
>
> "JLGWhiz" <JLGWhiz@cfl.rr.com> wrote in message
> news:u5\$pIN3yKHA.1796@TK2MSFTNGP02.phx.gbl...
>> Hi Rick, I ran a test and it did not error out.  But it does not hurt to
>> be safe.
>>
>>
>> "Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message
>> news:OZHMFv2yKHA.4752@TK2MSFTNGP04.phx.gbl...
>>> Your code will fail if the number is a whole number without a decimal
>>> point. If you change your If..Then statement to the following, then your
>>> code will work correctly...
>>>
>>> If Len(c) - InStr(c & ".", ".") > 2 Then
>>>
>>> --
>>> Rick (MVP - Excel)
>>>
>>>
>>>
>>> "JLGWhiz" <JLGWhiz@cfl.rr.com> wrote in message
>>> news:#sweMa2yKHA.4752@TK2MSFTNGP04.phx.gbl...
>>>> This worked pretty good.  You can adapt it to your needs.
>>>>
>>>> Sub dk()
>>>>
>>>>  For Each c In Range("A2:A6")
>>>>    If Len(c) - InStr(c, ".") > 2 Then
>>>>       MsgBox c.Address & "  More than 2 decimal places"
>>>>    End If
>>>>  Next
>>>> End Sub
>>>>
>>>>
>>>>
>>>> "zxcv" <zxcvnosend@yahoo.com> wrote in message
>>>> news:3466ddcc-9050-4873-9377-bcaee5b6517e@z3g2000yqz.googlegroups.com...
>>>>>I need to do some input validation so to check if a value has no more
>>>>> than 2 decimal digits.  So 14.12 is valid but 14.123 is not.
>>>>>
>>>>> I have tried doing a check like this:
>>>>>
>>>>>     If Int(inval * 100) <> inval * 100 Then
>>>>>
>>>>> but this gets a rounding error with certain values like 2.22.  If I
>>>>> subtract one side above from the other I get a difference on the order
>>>>> of 10^-21.
>>>>>
>>>>> So I tried rounding the numbers to do a test like this:
>>>>>
>>>>>     If Round(Int(inval * 100), 10) <> Round(inval * 100, 10) Then
>>>>>
>>>>> and this does something weird like rounding Round(Int(0.29 * 100), 10)
>>>>> to 28.
>>>>>
>>>>> Is there some simpler way to check that a number does not have too
>>>>> many decimal digits?
>>>>
>>>>
>>
>>

```
 0
JLGWhiz
3/24/2010 5:46:26 PM
```On Mar 24, 1:35=A0pm, "Joe User" <joeu2004> wrote:
> "zxcv" <zxcvnos...@yahoo.com> wrote:
>
> On Mar 24, 12:31 pm, "Joe User" <joeu2004> wrote:
>
> > > Try:
> > > If Round(inval,2) =3D inval Then
> [....]
> > Thanks for the input but I need to do this in VBA
> > as I have no control over the input and cannot put
> > any formulas in the sheet. =A0Someone else enters the
> > data and then another person hits a button that I
> > created.
>
> I don't understand your comment. =A0What I wrote is for VBA, and it is
> intended to deal with exactly the situation that you describe. =A0I think=
you
> misunderstand my comments. =A0Perhaps you should just give it a try.
>
> PS: =A0Sorry, I wrote "=3D" where you wanted "<>". =A0That's a simple cha=
nge, heh?
>
> ----- original message -----
>
> "zxcv" <zxcvnos...@yahoo.com> wrote in message
>
> news:6d74dc36-1fae-4269-8bba-dd366ae0b776@g28g2000yqh.googlegroups.com...
> On Mar 24, 12:31 pm, "Joe User" <joeu2004> wrote:
>
>
>
> > "zxcv" <zxcvnos...@yahoo.com> wrote:
> > > I need to do some input validation so to check if a
> > > value has no more than 2 decimal digits. So 14.12
> > > is valid but 14.123 is not.
>
> > Try:
>
> > If Round(inval,2) =3D inval Then
>
> > Normally, I would opt for WorksheetFunction.Round or even
> > Evaluate("round(...)") instead of the VB Round function. There are
> > functional differences. In this case, I do not think it makes a
> > difference.
> > Nevertheless, you might want to use one of those alternatives instead,
> > just
> > to be sure.
>
> > ----- original message -----
>
> > "zxcv" <zxcvnos...@yahoo.com> wrote in message
>
> >news:3466ddcc-9050-4873-9377-bcaee5b6517e@z3g2000yqz.googlegroups.com...
>
> > >I need to do some input validation so to check if a value has no more
> > > than 2 decimal digits. So 14.12 is valid but 14.123 is not.
>
> > > I have tried doing a check like this:
>
> > > If Int(inval * 100) <> inval * 100 Then
>
> > > but this gets a rounding error with certain values like 2.22. If I
> > > subtract one side above from the other I get a difference on the orde=
r
> > > of 10^-21.
>
> > > So I tried rounding the numbers to do a test like this:
>
> > > If Round(Int(inval * 100), 10) <> Round(inval * 100, 10) Then
>
> > > and this does something weird like rounding Round(Int(0.29 * 100), 10=
)
> > > to 28.
>
> > > Is there some simpler way to check that a number does not have too
> > > many decimal digits?
>
> Thanks for the input but I need to do this in VBA as I have no control
> over the input and cannot put any formulas in the sheet. =A0Someone else
> enters the data and then another person hits a button that I created.

I see!  I did not know that the WorksheetFunction object existed.  I
will make use of this more in the future.

Thank you.
```
 0
zxcv
3/25/2010 2:05:30 PM
```On Mar 24, 1:30=A0pm, "Joe User" <joeu2004> wrote:
> Embellishment....
>
> "zxcv" <zxcvnos...@yahoo.com> wrote:
> > I tried rounding the numbers to do a test like this:
> > If Int(inval * 100) <> inval * 100 Then
>
> > but this gets a rounding error with certain values like 2.22.
>
> The reason that does not work is because most numbers with decimal fracti=
ons
> cannot be represented exactly. =A0Instead, they are represented by a sum =
of 53
> consecutive powers of two (bits), for example 2*2^1 + 0*2^0 + 0*2^-1 +
> 0*2^-2 + 1*2^-3 + etc.
>
> Consequently, 2.22 is represented by exactly
> 2.22000000000000,0195399252334027551114559173583984375. =A0Int(2.22*100) =
is
> exactly 222. =A0But 2.22*100 is
> 222.000000000000,028421709430404007434844970703125, preserving the
> additional bits used to approximate 0.22 in this context.
>
> In contrast, Round(inval,2) results in inval exactly as it would be
> represented internally if it were entered with 2 decimal places. =A0So if
> inval is 2.22, Round(inval,2) results in
> 2.22000000000000,0195399252334027551114559173583984375. =A0But if inval w=
ere
> 2.22+2^-51 (the smallest value larger than 2.22), it would be represented
> internally as 2.22000000000000,06394884621840901672840118408203125, and
> Round(inval,2) does not equal inval.
>
> Note: =A0You cannot enter the
> 2.2200000000000006394884621840901672840118408203125 as a constant in Exce=
l;
> however, it can be the result of a calculation. =A0Also, you can enter th=
at
> constant in VBA, including as input to an InputBox. =A0Caveat: =A0If you =
write
> that constant in a VBA statement, the VBA editor might change it later wh=
en
> you edit the line. =A0It would be more reliable to write
> Cdbl("2.2200000000000006394884621840901672840118408203125").
>
> ----- original message -----
>
> "Joe User" <joeu2004> wrote in message
>
> news:%233Lsx92yKHA.5036@TK2MSFTNGP02.phx.gbl...
>
> > "zxcv" <zxcvnos...@yahoo.com> wrote:
> >> I need to do some input validation so to check if a
> >> value has no more than 2 decimal digits. =A0So 14.12
> >> is valid but 14.123 is not.
>
> > Try:
>
> > If Round(inval,2) =3D inval Then
>
> > Normally, I would opt for WorksheetFunction.Round or even
> > Evaluate("round(...)") instead of the VB Round function. =A0There are
> > functional differences. =A0In this case, I do not think it makes a
> > difference. Nevertheless, you might want to use one of those alternativ=
es
> > instead, just to be sure.
>
> > ----- original message -----
>
> > "zxcv" <zxcvnos...@yahoo.com> wrote in message
> >news:3466ddcc-9050-4873-9377-bcaee5b6517e@z3g2000yqz.googlegroups.com...
> >>I need to do some input validation so to check if a value has no more
> >> than 2 decimal digits. =A0So 14.12 is valid but 14.123 is not.
>
> >> I have tried doing a check like this:
>
> >> =A0 =A0 If Int(inval * 100) <> inval * 100 Then
>
> >> but this gets a rounding error with certain values like 2.22. =A0If I
> >> subtract one side above from the other I get a difference on the order
> >> of 10^-21.
>
> >> So I tried rounding the numbers to do a test like this:
>
> >> =A0 =A0 If Round(Int(inval * 100), 10) <> Round(inval * 100, 10) Then
>
> >> and this does something weird like rounding Round(Int(0.29 * 100), 10)
> >> to 28.
>
> >> Is there some simpler way to check that a number does not have too
> >> many decimal digits?

Thanks.  That is a great bit of information.  I never knew why the
rounding errors happened.
```
 0
zxcv
3/25/2010 2:06:15 PM
 Reply:

Similar Artilces:

number rounding in MS Excel
Hi, This may have been answered many times. In MS EXCEL is there a way that it calculates all numbers to 2 decimal digit accuracy instead of the 15 digit default? Thanks in advance for the answer. Hi You can set your decimal places in your cells as 2 and then check Precision As Displayed on the Tools / Options / Calculation page. Be careful though, it means what it says!! -- Andy. "mahusain" <abidh@bdnet.net> wrote in message news:ab59c6f6.0404200315.5196e8aa@posting.google.com... > Hi, > > This may have been answered many times. > > In MS EXCEL is ther...

Problems with creating a newsletter
I chose the form "Newsletter - email". I have created a 3 page newsletter. Now, I have NO IDEA how to send it out as an email. I do not want to send it as an attachment. I cannot figure out which "save as" format or what I need to do so that i can email this newsletter. Is there a website that goes through how to do this? Am I correct when I say that it has to be html in order for me to send it as email? That is not one of the options. Any help you give me would be greatly appreciated. Thank you, Markis www.adreamforabetterworld.com ...

incorrect check / cheque number printing
When generating a cheque run, let’s say cheque 61, 62, 63 and 64, the information printed on cheque # 62 is actually cheque # 64 and vice versa. This does not happen every cheque run, but it does happen fairly often. I have not seen GP do this. What version and service pack of GP are you using? Are you 100% sure the check stock itself is not out of sequence? Do you have any customizations? -- Victoria Yudin Dynamics GP MVP Flexible Solutions, Inc. "Syed" <Syed@discussions.microsoft.com> wrote in message news:3B02ECEF-7436-4CF3-930E-AA4D2DFDF117@microsoft.com... ...

VBA to check for latest version of front end?
I have a split database where the users have a local copy of the frontend. I would like to use VBA to check whether the users have the mostrecent version. I have set up the following tables:tVersion-Back-End (link to table in the BackEnd version)tVersion-FrontEnd (local table in the FrontEnd version)Each has a field Version where I am planning to put in the versionnumbers.Now, I am just starting to use VBA in my Access projects. Can someonepoint me to some coding to accomplish my task above? Can someone alsopoint me to a good resource (book, website, course) that would take mefrom a beginn...

Meeting updates #2
My users cannot update meetings created when they were on the old email server. I have noticed that the old string is still mapped to the meeting. e.g x400;c=us;a= ;p=Org name;o=exchagne;s=Lastname;g=firstname; Take a look at the following article: 275134 XADM: Cannot Reply to Messages That Are Sent from a User Account That http://support.microsoft.com/?id=275134 The same thing applies to meetings. How did you move them and what version(s) of Exchange? Thanks, Richard Roddy Microsoft Exchange Support This posting is provided "AS IS" with no warranties, and confers no ri...

Changing SQL Server for CRM 1.2
As my SQL server is currently on its last breath I am in the process of moving our CRM 1.2 databases to a new dedicated serever. I have detatched the databases from the old server, copied the files to the server and attached them. I have then gone into CRM deployment manager and changed to the SQL server to the new server. That all appears to work fine, no error messages. But when users try to access the CRM system they get an error and the CRM server event log fills with error messages. A few are listed below: dmLog: Failed to execute the SQL batch in the file sqlbatch.sql. MSCRM P...

How do you turn off/on the "1" or "2" or "+" or "-" view hide/unh.
What is or how do you control, turn on/off the small "1" "2" or "+" "-" hide/unhide view buttons right above the "A" cell? Sometimes shows when I hide columns. Hi 'Data - Goup/Outline' -- Regards Frank Kabel Frankfurt, Germany Seth wrote: > What is or how do you control, turn on/off the small "1" "2" or "+" > "-" hide/unhide view buttons right above the "A" cell? Sometimes > shows when I hide columns. You can toggle them to hide or display with <Ctrl> <8> Usi...

printing 2 charts on single page
Using Excel 2k, Win 2k I was trying to print to line charts on a single page to demonstrate the corelation between the two data sets. I would like to print in portait mode with one chart on top of page and one on bottom. Excel only seems to offer putting chart 2 on a separate page or embedding it in chart one. I am not very familiar with excel charting capabilites can anyone suggest a resorce? Thanks ...

Steps to Share Outlook on 2 different drives
This is a problem created by a dual boot of XPPSP2 on one drive C:\ and Vista on another drive E:\ that I use to format for new builds of Vista on the same box. I want to take my Outlook 2003 in box and folders on two different drives on one box and combine them so that all the emails go to one account and all the folders can be shared or used on each drive. I want to do the same with Outlook Express as well, and since this is an *Outlook newsgroup, I'll just take what I learn here and apply it to the ..dbx folder(s) in it. The idea is to receive email on both drives in one in one...

Can I show a number like 1,935,674,923 as 1.9B?
I would like to reformat 10- and 11-digit the numbers (e.g. like 1,935,674,923 as 1.9B). Can this be done? Custom format: 0.0,,,"B" best wishes -- Bernard Liengme Microsoft Excel MVP http://people.stfx.ca/bliengme "Dave" <Dave@discussions.microsoft.com> wrote in message news:262241F3-063E-4F97-B864-A629EC670112@microsoft.com... > I would like to reformat 10- and 11-digit the numbers (e.g. like > 1,935,674,923 as 1.9B). Can this be done? Dave, Use a custom format of #.#,,,"B" or 0.0,,,"B" depending on ho...

Offline folder problems #2
Hi Guys, Basically I installed Outlook 97 on my office PC which is connect to microsoft exchange server. For some reason all of my messages were copied into offline folders, this causes a problem, whenever I open up outlook I get this message. Upload of offline changes could not be completed you do not have sufficient permission to perform this operation on this object. See the folder contact or see your system administrator. I do not administrator access on my user profile, every time I open outlook it resets everything I have changed (view etc) and gives that error message. How ...

Receiving Transaction Entry #2
I went into Receiving Transaction Entry to record the receipt of an item. The invoiced price did not agree with the purchase order. I had to add Shipping and adjust the provincial tax. I went to the proper screen to over-ride the calculated tax and the system told me that I couldn't do that because I had not filled in all the BOLD, RED areas. At that point I wanted to close down that screen, but the system would not let me do that either. I could minimize the screen and then I could also see that there wasn't any required information that had not be entered. The only way I c...

Opening pub files created with older Publisher versions #2
I have just upgraded to Publisher 2003 from 2000 and am having trouble with pub files sent to me for our chuch newsletter which is using Publisher 97 The text is not wrapping around graphics boxes. Can I fix this? I really don't want to go back to Pub 200 Thank Richard this is caused by Publisher 97 not been printer independent. Even if you went back to Publisher 2000, unless you have the identical font versions and printer driver you would have issues with formatting. It sounds like the person sending you the file has a garbage HP inkjet printer. Get them to install the HP5P laser p...

Phone Number Filtering
I am trying to sort special numbers in a long list of telephone numbers (7 digits). For example: a. Highlight yellow those numbers which have 3 consecutive digits (eg 2281555). b. Highlightt blue those numbers which have two double numbers (eg 4431122). c. Highlight green those numbers which are in a certain sequence (eg 2281234). I tried to use Left, Right and Mid formulas but the game becomes very complicated and hard to troubleshoot. Any better solution? The only effective way I know how to do this, is to use macros. Your samples a & b, are fairly simple to do. Sample c, could be d...

Associating Multiple Existing Contacts to an Account, Until CRM 2.x
I have racked my pea-sized brain for endless days for a way to associate multiple existing contacts (or accounts) to an account. My client has accounts that have many contacts (or accounts) involved at different stages of acquiring a new account. The logical method would be to add a custom attribute with lookup functionality into the existing CRM contact or account objects but this is not possible with CRM 1.x. The resulting UI would allow an end user to associate different contacts (or accounts) to an account. Example: Account: ABC - AccountPhysician: (existing account or con...

Fractions Number format
Is it possible to do a custom number format for cells such that fractions are displayed. I'm guessing not because there'd be a limit on what fractions you could display, but I would probably only be interested in showing a half as the little 1/2 (alt,0189 I think) rather than .5 --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.478 / Virus Database: 275 - Release Date: 06/05/03 Lee There are a limited number of fractions that can be shown under Format>Cells>Fractions. Gord Dibben Excel MVP - XL97 SR2 & ...

VCard issue #2
This is a weird problem, but when I send a vcard to someone, it shows up in my sent items as a vcf file, and has a vcard icon on it, but when the recipient gets the VCard, it shows up as msg file, with an envelope icon as the attachment. When I open it, nothing is there - it's empty. I can't find anything regarding this problem on google or MS's website. Anybody have any ideas? Is it a problem with Outlook? Do I need to do a detect and repair, a reinstallation? Thanks for your help! Does the same happen when you send a message to yourself? Is he/she able to see it w...

Some Excel formatting functions taking a long time to work #2
Excel 2000 SP3 When I use some formatting functions for the first time in a session, such as bold, increasing font size etc, it takes up to 30 seconds to work. Meantime Excel is locked up until it completes that formatting call. I suspect faulty DLL? Has anyone experienced this? How to fix (other than a complete re- install) ? Any advice appreciated. Piri On Nov 5, 8:39=A0am, Piri <wiremu.pare...@hotmail.com> wrote: > Excel 2000 SP3 > When I use some formatting functions for the first time in a > session, such as bold, increasing font size etc, it takes =A0up to 30 > secon...

Customer check data
When customers pay by check RMS asks for specific information such as drivers license number, routing number, account number, address and phone number. Does anyone have a report and or a way to extract this info from the database for cases when the check is returned for NSF? Please advise, Scott We can write you this report. Contact me for detail on price . Afshin Alikhani - [ afshin@retailrealm.co.uk ] CEO - Retail Realm = = = = = = = = = = "Scott Santorio" <scott@tt-newyork.com> wrote in message news:e8ZKkR6\$HHA.3716@TK2MSFTNGP03.phx.gbl... > When customers pa...

Rounding Numbers #3
I have a list of values as below: 476.14 361.99 345.69 463.08 515.29 403.44 330.68 347.64 375.36 I would like to create a formula that rounds the values to the nearest 0.05 eg. Round 476.14 to 476.15, 361.99 to 362.00, 375.36 to 375.35 etc… Is there anyway that I can do this? Thanks, Jane. JaneC wrote: > I have a list of values as below: > 476.14 > 361.99 > 345.69 > 463.08 > 515.29 > 403.44 > 330.68 > 347.64 > 375.36 > I would like to create a formula that rounds the values > to the nearest 0.05 eg. Round 476.14 to 476.15, > 361.99 to 362.00, 375.36...

Roman and Arabic numbers in SEQ
Running 2003 on XP. Got a doc that has SEQ numbering. The code is: ARTICLE{seq level0\h\r0}{seq level1\h\r0} etc. to level 7 and the last entry is {seq level0\r1\*ROMAN) This displays ARTICLE I The next level down is has this code: {seq level0\c\*ROMAN}.{seq level1\r1 \*arabic} This displays as : I.1, with numbers until article five displaying as V.1, V.2, etc. I want it to display as 1.1, 1.2, etc. I tried replacing the ROMAN with arabic, but it didn't change anything. I have spent hours trying to figure this out. Using reveal formating and styles, they are no styles...

Invoice Numbers 10-27-07
We produce reports that are invoices.. The reports are really a group of compined reports if this matters... When we print the reports I would like to have printed consecutive invoice numbers. If possible I would like to have the number apprear as AS-00001, AS-00002 ect.. I am not really interested in storing the invoice numbers I just need them on the printed invoice as it is made of of groups of various data that is stored... Thank In Advance for you help. Bob If you just want a consequetive numbering on the report, all with an AS- prefix, see: Numbering Entries in a Report o...

2 Domains, 1 Exchange Server
Hi, We're trying to go with 2 seperate 2003 domains, but with only one Exchange 2003 Server. What would be needed for the domain that the Exchange server is not in to access e-mail? We've contemplated having those users use OWA, but would really like them to be able to use Outlook 2003. We're a school district and want to segregate the kids domain from the administration domain, but only have funding for one Exchnage server. Any ideas\help will be appreciated. TIA. Hi, This should explain what it requires: http://support.microsoft.com/?id=278888 Leif "tj woo...

Creating a Macro to Delete Commas #2
I have an excel file that the size will varry. I need a macro that will check all the fields for a comma. If there is one I would like to get rid of it. Does anyone have any idea how to do this? I have no idea and I have been assigned this task. Help --- Message posted from http://www.ExcelForum.com/ No macro required. ctrl-H for find/replace. find , replace nothing (leave the replace field blank). You can of course record that within a macro if you wish. Drabbacs >-----Original Message----- >I have an excel file that the size will varry. I need a macro that will >check ...

how do I import my contacts from IncrediMail 2.0 to MOutlook2007
I am new to using microsoft outlook 2007 and am searching for a way to upload my contact list, from Incredimail 2.0 to Outlook 2007. Any help would be definitely appreciated, thanks! How do I save my IncrediMail Address Book and move it to a different location? http://help.incredimail.com/incredimail/help_center/help_article.aspx?is=t&article_id=67&lang_id=9 In Outlook - File --> Import and Export ... import the CSV file created via process noted above Karl -- ____________________________________________________________ Karl Timmermans - The Claxton Group Co...