Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 28
Default Test for fractions of a penny

I'm attempting to write code to determine if the user entered a value
that has a fraction of a cent. Here is my logic

dim i as long
dim d as double

If IsNumeric(.Value) = True And .Value < 0 Then
.Value = Abs(.Value)
i = CLng(CDbl(.Value) * 100)
d = CDbl(.Value) * 100
If Abs(d - i) 0 Then
MsgBox "Rounding error detected! Make sure you don't have umbers
with values less than 1/100th", vbExclamation
End If
Else
.Value = ""
End If

It seems to work for most cases however I'd found some specific values
that always report "Round error detected!...."

Values such as 282.47, 32.41 and 132.70

If I MsgBox i & " " & d the numbers are the same so
Abs(d - i) 0 should always be false I don't quite understand what's
doing on. Is there a better way to test for fractions of a cent?

Eric

  #2   Report Post  
Posted to microsoft.public.excel.programming
Roy Roy is offline
external usenet poster
 
Posts: 53
Default Test for fractions of a penny

Eric,

How about multiplying the test value by 100, subtract the integer portion
and test whats left against zero.

eg: 100.47 x 100 = 10047
10047 -int(10047) = 0 ' the value is ok

100.472 x 100 = 10047.2
10047.2 - int(10047.2) = .2 ' user enter a fraction of a cent

Roy
"Wescotte" wrote:

I'm attempting to write code to determine if the user entered a value
that has a fraction of a cent. Here is my logic

dim i as long
dim d as double

If IsNumeric(.Value) = True And .Value < 0 Then
.Value = Abs(.Value)
i = CLng(CDbl(.Value) * 100)
d = CDbl(.Value) * 100
If Abs(d - i) 0 Then
MsgBox "Rounding error detected! Make sure you don't have umbers
with values less than 1/100th", vbExclamation
End If
Else
.Value = ""
End If

It seems to work for most cases however I'd found some specific values
that always report "Round error detected!...."

Values such as 282.47, 32.41 and 132.70

If I MsgBox i & " " & d the numbers are the same so
Abs(d - i) 0 should always be false I don't quite understand what's
doing on. Is there a better way to test for fractions of a cent?

Eric


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Test for fractions of a penny

Eric,

How about just forcing the numbers to two decimal places?
'-------------------------
Sub xxx()
Dim dblNum As Double
dblNum = 282.47123

If IsNumeric(dblNum) = True And dblNum < 0 Then
dblNum = Abs(WorksheetFunction.Round(dblNum, 2))
MsgBox dblNum
Else
MsgBox "Your entry should be a number greater than 0. "
End If
End Sub
'-----------------------

Regards,
Jim Cone
San Francisco, USA


"Wescotte" wrote in message
oups.com...
I'm attempting to write code to determine if the user entered a value
that has a fraction of a cent. Here is my logic

dim i as long
dim d as double

If IsNumeric(.Value) = True And .Value < 0 Then
.Value = Abs(.Value)
i = CLng(CDbl(.Value) * 100)
d = CDbl(.Value) * 100
If Abs(d - i) 0 Then
MsgBox "Rounding error detected! Make sure you don't have umbers
with values less than 1/100th", vbExclamation
End If
Else
.Value = ""
End If
It seems to work for most cases however I'd found some specific values
that always report "Round error detected!...."
Values such as 282.47, 32.41 and 132.70
If I MsgBox i & " " & d the numbers are the same so
Abs(d - i) 0 should always be false I don't quite understand what's
doing on. Is there a better way to test for fractions of a cent?
Eric

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 28
Default Test for fractions of a penny

That's what this code does..

i = CLng(CDbl(.Value) * 100)
d = CDbl(.Value) * 100
If Abs(d - i) 0 Then
' Entered value has a fraction of a cent

Try 282.47
if I add MsgBox i & " " & d & " " & .Value & " " & Abs(d - i) just
after the d = line above

the results are
28247 28247 282.47 3.6379788709171E-12

so basically Double(282.47 * 100) - Long(282.47 * 100) != 0

Not sure how to get around thing correctly..

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 28
Default Test for fractions of a penny

I thought of that but I need to keep the correct value in the cell and
just not let the user advance until they correct it manually because if
I just round everything it will cause a headache trying to figure out
where your extra penny went/came from.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Test for fractions of a penny

Eric,

Why not enter the rounded number into the cell?

Jim Cone

"Wescotte" wrote in message ups.com...
I thought of that but I need to keep the correct value in the cell and
just not let the user advance until they correct it manually because if
I just round everything it will cause a headache trying to figure out
where your extra penny went/came from.

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Test for fractions of a penny

Eric,

Also suggest you review this article...
http://www.cpearson.com/excel/rounding.htm

Regards,
Jim Cone
San Francisco, USA


"Wescotte" wrote in message ups.com...
I thought of that but I need to keep the correct value in the cell and
just not let the user advance until they correct it manually because if
I just round everything it will cause a headache trying to figure out
where your extra penny went/came from.

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 129
Default Test for fractions of a penny

Hi,
how about this:

If CStr(CLng(.Value * 100) / 100) < CStr(.Value) Then

--
HTH
okaizawa


Wescotte wrote:
I'm attempting to write code to determine if the user entered a value
that has a fraction of a cent. Here is my logic

dim i as long
dim d as double

If IsNumeric(.Value) = True And .Value < 0 Then
.Value = Abs(.Value)
i = CLng(CDbl(.Value) * 100)
d = CDbl(.Value) * 100
If Abs(d - i) 0 Then
MsgBox "Rounding error detected! Make sure you don't have umbers
with values less than 1/100th", vbExclamation
End If
Else
.Value = ""
End If

It seems to work for most cases however I'd found some specific values
that always report "Round error detected!...."

Values such as 282.47, 32.41 and 132.70

If I MsgBox i & " " & d the numbers are the same so
Abs(d - i) 0 should always be false I don't quite understand what's
doing on. Is there a better way to test for fractions of a cent?

Eric

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calc sometimes off by a penny Access Joe Excel Worksheet Functions 6 April 29th 23 03:43 AM
Exel 2007 penny off scarter Excel Discussion (Misc queries) 4 April 10th 08 01:43 AM
AutoSum lost a penny PoldenKaz Excel Worksheet Functions 5 September 28th 07 01:30 PM
My accounting program is a penny off, Why? bigelwood Excel Worksheet Functions 2 April 23rd 07 01:46 AM
In Excel the total is off by a penny. jules Excel Worksheet Functions 1 June 17th 05 03:43 AM


All times are GMT +1. The time now is 08:39 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"