Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Ian Ian is offline
external usenet poster
 
Posts: 238
Default Number discrepency

I have a userform to prompt users to enter certain data before being allowed
access to a sheet for further data entry.

On this form I have a textbox for a software version in the format x.xx

The data in this textbox is then entered in a cell on the sheet using the
line below.

If UserForm1.ComboBox1.Value < "Other" Then Worksheets("Ziehm").Range("L2")
= CSng(TextBox1.Value) 'Software version

This works well except that the value transferred is not exactly the same as
that entered

eg I entered 4.16 in the textbox and, although 4.16 is shown in the sheet,
this is only due to cell formatting. The actual value transferred is
4.1599998474121.

Can anyone a) explain why, and b) suggest alternative/additional code such
that what is in the text box is transferred exactly? I'm more concerned with
the solution than the explanation, but it would be interesting to know why.

Due to other code this value MUST be a number.

TIA

--
Ian
--


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Number discrepency

the solution is to change

csng( )
to
cdbl( )

numbers in Excel are stored as Double.

When you convert the single to the double, you get the loss of precision
(demo'd from the immediate window):

? csng("4.16")
4.16
? cdbl("4.16")
4.16
? cdbl(csng("4.16"))
4.15999984741211

The root cause probably includes the storage specification of IEEE double
precision numbers.

--
Regards,
Tom Ogilvy




"Ian" wrote:

I have a userform to prompt users to enter certain data before being allowed
access to a sheet for further data entry.

On this form I have a textbox for a software version in the format x.xx

The data in this textbox is then entered in a cell on the sheet using the
line below.

If UserForm1.ComboBox1.Value < "Other" Then Worksheets("Ziehm").Range("L2")
= CSng(TextBox1.Value) 'Software version

This works well except that the value transferred is not exactly the same as
that entered

eg I entered 4.16 in the textbox and, although 4.16 is shown in the sheet,
this is only due to cell formatting. The actual value transferred is
4.1599998474121.

Can anyone a) explain why, and b) suggest alternative/additional code such
that what is in the text box is transferred exactly? I'm more concerned with
the solution than the explanation, but it would be interesting to know why.

Due to other code this value MUST be a number.

TIA

--
Ian
--



  #3   Report Post  
Posted to microsoft.public.excel.programming
Ian Ian is offline
external usenet poster
 
Posts: 238
Default Number discrepency

Easy when you know how :-)

Thanks Tom.

--
Ian
--
"Tom Ogilvy" wrote in message
...
the solution is to change

csng( )
to
cdbl( )

numbers in Excel are stored as Double.

When you convert the single to the double, you get the loss of precision
(demo'd from the immediate window):

? csng("4.16")
4.16
? cdbl("4.16")
4.16
? cdbl(csng("4.16"))
4.15999984741211

The root cause probably includes the storage specification of IEEE double
precision numbers.

--
Regards,
Tom Ogilvy




"Ian" wrote:

I have a userform to prompt users to enter certain data before being
allowed
access to a sheet for further data entry.

On this form I have a textbox for a software version in the format x.xx

The data in this textbox is then entered in a cell on the sheet using the
line below.

If UserForm1.ComboBox1.Value < "Other" Then
Worksheets("Ziehm").Range("L2")
= CSng(TextBox1.Value) 'Software version

This works well except that the value transferred is not exactly the same
as
that entered

eg I entered 4.16 in the textbox and, although 4.16 is shown in the
sheet,
this is only due to cell formatting. The actual value transferred is
4.1599998474121.

Can anyone a) explain why, and b) suggest alternative/additional code
such
that what is in the text box is transferred exactly? I'm more concerned
with
the solution than the explanation, but it would be interesting to know
why.

Due to other code this value MUST be a number.

TIA

--
Ian
--





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
Sumif number is greater than a number but less than another number lulu151 Excel Discussion (Misc queries) 2 May 7th 10 07:12 PM
How can I click on a telephone number in an Excel 2002 spreadsheet, and have the number dialed? jbclem Excel Discussion (Misc queries) 2 August 13th 09 01:57 AM
How to calculate number of occurencies of a specific number number Stefan Excel Discussion (Misc queries) 4 September 8th 08 08:33 AM
Transparency Discrepency Quahaug Excel Programming 8 October 18th 06 12:11 PM
Rounding a number to a multiple quantity that adds to a fixed total number wjlo Excel Worksheet Functions 1 November 9th 04 04:43 PM


All times are GMT +1. The time now is 10:15 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"