Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 201
Default Rounding & Formula Bar

Excel 2002, WinXP
I am helping an OP and I have run into a problem. The code calculates a
value (number), say 22.666667. The OP wants this number in some cell, but
rounded to one decimal place. He doesn't want the cell display rounded
(formatted), he wants the number itself rounded. No problem. The code
rounds the number and
places the rounded number in the cell and it appears in the cell as 22.7.
But the Formula Bar displays 22.7000007629394 and he wants the formula
bar to display the rounded number.
The following short macro demonstrates the problem.
Sub TestRound()
Dim Num As Single
Num = 22.666667
[A1] = Num
[A2] = Round(Num, 1)
Num = Round(Num, 1)
[A3] = Num
End Sub

The result of running this macro is:
A1 - 22.666667 in the cell, 27.66666679282324 in the formula bar
A2 - 22.7 in the cell, 22.7000007629394 in the formula bar
A3 - same as A2

I have 3 questions:
Why does A1 display 27.66666679282324 in the formula bar when the value of
Num is defined as 22.666667?
Why do A2 & A3 display 22.7000007629394 in the formula bar?
How can I get 22.7 to display in the formula bar for A2 & A3?

I am running out of hair. Thanks for your help. Otto


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default Rounding & Formula Bar

I'm confused as you are because I was sure it was ok...
and after trying it is ok (with my excel 2000)

but, you can change the formula this way:
[A1].formula = num

this will (sure ?) help !
I hope for you

( in fact I prefer to use Range("A1").formula, but... [] also works fine )



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Rounding & Formula Bar

On Sun, 25 Jul 2004 09:08:54 -0400, "Otto Moehrbach"
wrote:

Excel 2002, WinXP
I am helping an OP and I have run into a problem. The code calculates a
value (number), say 22.666667. The OP wants this number in some cell, but
rounded to one decimal place. He doesn't want the cell display rounded
(formatted), he wants the number itself rounded. No problem. The code
rounds the number and
places the rounded number in the cell and it appears in the cell as 22.7.
But the Formula Bar displays 22.7000007629394 and he wants the formula
bar to display the rounded number.
The following short macro demonstrates the problem.
Sub TestRound()
Dim Num As Single
Num = 22.666667
[A1] = Num
[A2] = Round(Num, 1)
Num = Round(Num, 1)
[A3] = Num
End Sub

The result of running this macro is:
A1 - 22.666667 in the cell, 27.66666679282324 in the formula bar
A2 - 22.7 in the cell, 22.7000007629394 in the formula bar
A3 - same as A2

I have 3 questions:
Why does A1 display 27.66666679282324 in the formula bar when the value of
Num is defined as 22.666667?
Why do A2 & A3 display 22.7000007629394 in the formula bar?
How can I get 22.7 to display in the formula bar for A2 & A3?

I am running out of hair. Thanks for your help. Otto


I suspect you are running into issues with precision. Try:

Dim Num as Double


--ron
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default Rounding & Formula Bar

I'm gonna guess it has more to do with the precision of a single and putting it
into a cell (which is treated as a double).

I changed:
Dim Num As Single
to
Dim Num As Double

and it worked ok.

For what it's worth, I don't use singles or integers.

Otto Moehrbach wrote:

Excel 2002, WinXP
I am helping an OP and I have run into a problem. The code calculates a
value (number), say 22.666667. The OP wants this number in some cell, but
rounded to one decimal place. He doesn't want the cell display rounded
(formatted), he wants the number itself rounded. No problem. The code
rounds the number and
places the rounded number in the cell and it appears in the cell as 22.7.
But the Formula Bar displays 22.7000007629394 and he wants the formula
bar to display the rounded number.
The following short macro demonstrates the problem.
Sub TestRound()
Dim Num As Single
Num = 22.666667
[A1] = Num
[A2] = Round(Num, 1)
Num = Round(Num, 1)
[A3] = Num
End Sub

The result of running this macro is:
A1 - 22.666667 in the cell, 27.66666679282324 in the formula bar
A2 - 22.7 in the cell, 22.7000007629394 in the formula bar
A3 - same as A2

I have 3 questions:
Why does A1 display 27.66666679282324 in the formula bar when the value of
Num is defined as 22.666667?
Why do A2 & A3 display 22.7000007629394 in the formula bar?
How can I get 22.7 to display in the formula bar for A2 & A3?

I am running out of hair. Thanks for your help. Otto


--

Dave Peterson

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 201
Default Rounding & Formula Bar

Ron, Dave
Thanks for your help. Changing the declaration to a Double did it.
And, Dave, I'm going to take your advise and stay away from Single and
Integer. Thanks again. Otto
"Otto Moehrbach" wrote in message
...
Excel 2002, WinXP
I am helping an OP and I have run into a problem. The code calculates a
value (number), say 22.666667. The OP wants this number in some cell, but
rounded to one decimal place. He doesn't want the cell display rounded
(formatted), he wants the number itself rounded. No problem. The code
rounds the number and
places the rounded number in the cell and it appears in the cell as 22.7.
But the Formula Bar displays 22.7000007629394 and he wants the formula
bar to display the rounded number.
The following short macro demonstrates the problem.
Sub TestRound()
Dim Num As Single
Num = 22.666667
[A1] = Num
[A2] = Round(Num, 1)
Num = Round(Num, 1)
[A3] = Num
End Sub

The result of running this macro is:
A1 - 22.666667 in the cell, 27.66666679282324 in the formula bar
A2 - 22.7 in the cell, 22.7000007629394 in the formula bar
A3 - same as A2

I have 3 questions:
Why does A1 display 27.66666679282324 in the formula bar when the value of
Num is defined as 22.666667?
Why do A2 & A3 display 22.7000007629394 in the formula bar?
How can I get 22.7 to display in the formula bar for A2 & A3?

I am running out of hair. Thanks for your help. Otto






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
ROUNDING FORMULA =ROUND(B12/$B$10,1) ROUNDING TO HIGH SunshineinFt.Myers[_2_] Excel Worksheet Functions 7 March 5th 09 06:41 PM
I need a formula with rounding up & rounding down to the nearest . Tony Kay Excel Worksheet Functions 3 May 29th 07 11:13 PM
Rounding with the sum formula pgruening Excel Discussion (Misc queries) 3 January 2nd 06 08:20 PM
rounding a formula in a cell, then replace the formula Don Guillett[_4_] Excel Programming 0 July 13th 04 03:59 PM
rounding a formula in a cell, then replace the formula Frank Kabel Excel Programming 0 July 13th 04 03:52 PM


All times are GMT +1. The time now is 11:49 PM.

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

About Us

"It's about Microsoft Excel"