473,499 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

mystery 2.3

I am working on a page with textboxes that can only accept a certain number
of decimals places. So I wrote a small function to check the number of
decimals, not counting zeros, as well as range check the entry and then
return the results, based on a parsing method I learned in c++ involving
integer division:

public bool entryCheck(int decimals, double min, double max, TextBox field,
Label error)
{
bool valid = false;
if (field.Text != "")
{
error.Text = "";
double entry = double.Parse(field.Text);
//decimal check
if ((double)((int)(entry * Math.Pow(10,(decimals + 1))) / 10) != entry *
Math.Pow(10, decimals))
{
field.ForeColor = Color.Red;
error.Text = "Too Many Decimals";
}
//range check
else if (entry < min || entry max)
{
field.ForeColor = Color.Red;
error.Text = "Invalid Range";
}
else
{
field.ForeColor = Color.Blue;
valid = true;
}
}
else
error.Text = "Required Field"; //error if left blank

return valid;
}

It works beautifully ALMOST all of the time. For some reason, when the entry
is 2.3, the computer multiplies it by say 10000 and gets 22999.9999, which
obviously won't work and isn't correct. Does anyone know why this is?
Thanks?

--
Message posted via http://www.dotnetmonster.com

Jul 11 '07 #1
4 1336
because floating point is done in base 2, not base 10. just as 1/3 is
..33333... in base 10, 2.3 is a repeating number in base 2

switch to the decimal datatype which uses base 10 (actual its integer
only with a implied decimal).
-- bruce (sqlwork.com)

Vince13 via DotNetMonster.com wrote:
I am working on a page with textboxes that can only accept a certain number
of decimals places. So I wrote a small function to check the number of
decimals, not counting zeros, as well as range check the entry and then
return the results, based on a parsing method I learned in c++ involving
integer division:

public bool entryCheck(int decimals, double min, double max, TextBox field,
Label error)
{
bool valid = false;
if (field.Text != "")
{
error.Text = "";
double entry = double.Parse(field.Text);
//decimal check
if ((double)((int)(entry * Math.Pow(10,(decimals + 1))) / 10) != entry *
Math.Pow(10, decimals))
{
field.ForeColor = Color.Red;
error.Text = "Too Many Decimals";
}
//range check
else if (entry < min || entry max)
{
field.ForeColor = Color.Red;
error.Text = "Invalid Range";
}
else
{
field.ForeColor = Color.Blue;
valid = true;
}
}
else
error.Text = "Required Field"; //error if left blank

return valid;
}

It works beautifully ALMOST all of the time. For some reason, when the entry
is 2.3, the computer multiplies it by say 10000 and gets 22999.9999, which
obviously won't work and isn't correct. Does anyone know why this is?
Thanks?
Jul 11 '07 #2
I prefer this rather simplistic approach

Double start_value = 5231.327000;
string[] end_values = start_value.ToString().Split('.');
int fubar = (end_values.Length 1) ? end_values[1].Length : 0;
Response.Write(fubar.ToString());

Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog

"bruce barker" <no****@nospam.comwrote in message
news:Ob*************@TK2MSFTNGP06.phx.gbl...
because floating point is done in base 2, not base 10. just as 1/3 is
.33333... in base 10, 2.3 is a repeating number in base 2

switch to the decimal datatype which uses base 10 (actual its integer only
with a implied decimal).
-- bruce (sqlwork.com)

Vince13 via DotNetMonster.com wrote:
>I am working on a page with textboxes that can only accept a certain
number
of decimals places. So I wrote a small function to check the number of
decimals, not counting zeros, as well as range check the entry and then
return the results, based on a parsing method I learned in c++ involving
integer division:

public bool entryCheck(int decimals, double min, double max, TextBox
field,
Label error)
{
bool valid = false;
if (field.Text != "")
{
error.Text = "";
double entry = double.Parse(field.Text);
//decimal check
if ((double)((int)(entry * Math.Pow(10,(decimals + 1))) / 10) != entry *
Math.Pow(10, decimals))
{
field.ForeColor = Color.Red;
error.Text = "Too Many Decimals";
}
//range check
else if (entry < min || entry max)
{
field.ForeColor = Color.Red;
error.Text = "Invalid Range";
}
else
{
field.ForeColor = Color.Blue;
valid = true;
}
}
else
error.Text = "Required Field"; //error if left blank

return valid;
}

It works beautifully ALMOST all of the time. For some reason, when the
entry
is 2.3, the computer multiplies it by say 10000 and gets 22999.9999,
which
obviously won't work and isn't correct. Does anyone know why this is?
Thanks?

Jul 11 '07 #3
Thanks a lot guys!

**in case anyone else is wondering:

if ((((int)(dentry * (decimal)Math.Pow(10,(decimals + 1)))) / 10) != dentry *
(decimal)Math.Pow(10, decimals))

gave me the corrext result

--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...p-net/200707/1

Jul 12 '07 #4
On Wed, 11 Jul 2007 19:19:34 GMT, "Vince13 via DotNetMonster.com"
<u35350@uwewrote:
>I am working on a page with textboxes that can only accept a certain number
of decimals places. So I wrote a small function to check the number of
decimals, not counting zeros, as well as range check the entry and then
return the results, based on a parsing method I learned in c++ involving
integer division:

public bool entryCheck(int decimals, double min, double max, TextBox field,
Label error)
{
bool valid = false;
if (field.Text != "")
{
error.Text = "";
double entry = double.Parse(field.Text);
//decimal check
if ((double)((int)(entry * Math.Pow(10,(decimals + 1))) / 10) != entry *
Math.Pow(10, decimals))
{
field.ForeColor = Color.Red;
error.Text = "Too Many Decimals";
}
//range check
else if (entry < min || entry max)
{
field.ForeColor = Color.Red;
error.Text = "Invalid Range";
}
else
{
field.ForeColor = Color.Blue;
valid = true;
}
}
else
error.Text = "Required Field"; //error if left blank

return valid;
}

It works beautifully ALMOST all of the time. For some reason, when the entry
is 2.3, the computer multiplies it by say 10000 and gets 22999.9999, which
obviously won't work and isn't correct. Does anyone know why this is?
Thanks?
What I would suggest is to use a numeric up down control. This yields
a couple of benefits
- Less code
- Less headache
- Simpler to understand
- Intuitive to the users

--
http://bytes.thinkersroom.com
Jul 14 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1958
by: Red | last post by:
In netscape bookmark files, there are lots of lines like this: <DT><A HREF="http://www.commondreams.org/" ADD_DATE="1091500674" LAST_CHARSET="ISO-8859-1" ID="rdf:#$uiYyb3">Common Dreams</A> I...
12
2771
by: Raymond Hettinger | last post by:
For your amusement and edification, I'm working on a series of Python puzzles designed to highlight areas of the language known only to those who have read the docs more than once. Each of the...
0
1864
by: Francis Avila | last post by:
A few days ago (see the 'itertools.flatten()?' thread from October 28) I became obsessed with refactoring a recursive generator that yielded the leaves of nested iterables. When the dust settled,...
0
1168
by: Morris Carré | last post by:
Back then - in particular, soon after 9/11 - I evidenced the natural irony of the standard python implementation, by showing it had the clear and spontaneous humor of replying : 'BUSH' when...
3
1778
by: Jim Geissman | last post by:
I have function that returns a table of information about properties. The data comes from three different tables -- addresses (called PropertyID), property characteristics, and events concerning...
5
2189
by: BLaci | last post by:
Please take a look at the following drop in ad: http://www.traficdublu.ro/dev/dropin.htm The script is written in the http://www.traficdublu.ro/dev/amcuceba.js . I want to call the dropin.htm...
0
1748
by: William Wisnieski | last post by:
Hello Everyone: I'm having a very strange problem occurring with my Access 2000 database. I call it the "mystery record." Here's the story: I have a query by form that returns a record set...
115
7462
by: Mark Shelor | last post by:
I've encountered a troublesome inconsistency in the C-language Perl extension I've written for CPAN (Digest::SHA). The problem involves the use of a static array within a performance-critical...
14
2750
by: jojoba | last post by:
Hi, I hope this post is ok for this group. Here's my deal: I have two computers on my LAN at home. One desktop. One laptop. Both computers are wireless enabled (and wired enabled too). I...
0
7130
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7386
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5468
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4918
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.