473,399 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Decimal precision ?

Hi

I'm working with Datagrid containg decimal (editable) numbers. The
user-entered numbers are read by decimal.Parse() method. However, Datagrid
formats some decimals as "1", but some as (eg) "1.00" - depends on what the
user had entered. The page has DISABLED viewstate.

So, let someone explain me why the number 1 is not the same as 1.0 or 1.000?
I cannot find any property to set the precision.

The number of decimal digits can be different in different rows, so no
static format option can be used. Currently the only option to get correct
formatting is (a bit stupid) construction like this:
((decimal)DataBinder.Eval(Container.DataItem, "Quantity")).ToString("F" +
((int)DataBinder.Eval(Container.DataItem, "DecimalPlaces")).ToString()

Regards.

Nov 18 '05 #1
2 6482
Hi, Piotr,

"Piotr" <an******@false.account.pl> wrote in message
news:c4**********@atlantis.news.tpi.pl...
Hi

I'm working with Datagrid containg decimal (editable) numbers. The
user-entered numbers are read by decimal.Parse() method. However, Datagrid
formats some decimals as "1", but some as (eg) "1.00" - depends on what the user had entered. The page has DISABLED viewstate.

So, let someone explain me why the number 1 is not the same as 1.0 or 1.000?

There is no difference *between the values* indeed. The difference is in the
way these are stored.

1 is stored as 1 / 10^0
1.0 is stored as 10 / 10^1
1.000 is stored as 1000 / 10^3

See:
http://msdn.microsoft.com/library/en...ClassTopic.asp

This code illustrates it:

decimal d = decimal.One;
for(int i = 0; i < 20;i++)
{
d = -decimal.Parse(d.ToString("F" + i));
int[] strip = decimal.GetBits(d);
ulong value = Convert.ToUInt64(
((uint)strip[1]).ToString("x") +
((uint)strip[0]).ToString("x"), 16);
Console.WriteLine(
"{0,-22} = {1,-22} / 10^{2,-3} {3}",
d,
value,
(strip[3] & 0xFF0000) >> 16,
(((strip[3] >> 31) & 1) > 0 ? "Negative" : "Positive"));
}

See the topic on GetBits method:
http://msdn.microsoft.com/library/en...tBitsTopic.asp
I cannot find any property to set the precision.

The number of decimal digits can be different in different rows, so no
static format option can be used. Currently the only option to get correct
formatting is (a bit stupid) construction like this:
((decimal)DataBinder.Eval(Container.DataItem, "Quantity")).ToString("F" +
((int)DataBinder.Eval(Container.DataItem, "DecimalPlaces")).ToString()
You can try setting the DataFormatString property alternatively:

"F" + DataBinder.Eval(Container.DataItem, "DecimalPlaces").ToString()

should be fine.

Hope this helps
Martin

Regards.

Nov 18 '05 #2
Hi, Piotr,

"Piotr" <an******@false.account.pl> wrote in message
news:c4**********@atlantis.news.tpi.pl...
Hi

I'm working with Datagrid containg decimal (editable) numbers. The
user-entered numbers are read by decimal.Parse() method. However, Datagrid
formats some decimals as "1", but some as (eg) "1.00" - depends on what the user had entered. The page has DISABLED viewstate.

So, let someone explain me why the number 1 is not the same as 1.0 or 1.000?

There is no difference *between the values* indeed. The difference is in the
way these are stored.

1 is stored as 1 / 10^0
1.0 is stored as 10 / 10^1
1.000 is stored as 1000 / 10^3

See:
http://msdn.microsoft.com/library/en...ClassTopic.asp

This code illustrates it:

decimal d = decimal.One;
for(int i = 0; i < 20;i++)
{
d = -decimal.Parse(d.ToString("F" + i));
int[] strip = decimal.GetBits(d);
ulong value = Convert.ToUInt64(
((uint)strip[1]).ToString("x") +
((uint)strip[0]).ToString("x"), 16);
Console.WriteLine(
"{0,-22} = {1,-22} / 10^{2,-3} {3}",
d,
value,
(strip[3] & 0xFF0000) >> 16,
(((strip[3] >> 31) & 1) > 0 ? "Negative" : "Positive"));
}

See the topic on GetBits method:
http://msdn.microsoft.com/library/en...tBitsTopic.asp
I cannot find any property to set the precision.

The number of decimal digits can be different in different rows, so no
static format option can be used. Currently the only option to get correct
formatting is (a bit stupid) construction like this:
((decimal)DataBinder.Eval(Container.DataItem, "Quantity")).ToString("F" +
((int)DataBinder.Eval(Container.DataItem, "DecimalPlaces")).ToString()
You can try setting the DataFormatString property alternatively:

"F" + DataBinder.Eval(Container.DataItem, "DecimalPlaces").ToString()

should be fine.

Hope this helps
Martin

Regards.

Nov 18 '05 #3

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

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
6
by: Peter Blatt | last post by:
Does 5 represent the total numer of digits (including the fractional portion) or only the number of places BEFORE the decimal point? Moreover does the number include the decimal point? Are there...
1
by: Tariq | last post by:
I've a SQL view performing a number of very precise calculations ie. 495/(1.112 - (.00043499*((Col1 + Col2 + Col3))) + ( .00000055 * ((Col1 + Col2 + Col3)) * ((Col1 + Col2 + Col3))) - (.00028826...
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
8
by: nick | last post by:
printf("%lf",3.25); the result is 3.25000 i want the answer correct to 3 decimal places What should i do? thanks!
10
by: Paul Sullivan | last post by:
decimal d; d = 1.1M OR d= (decimal) 1.1 Discussioon
11
by: Pieter | last post by:
Hi, I'm having some troubles with my numeric-types in my VB.NET 2005 application, together with a SQL Server 2000. - I first used Single in my application, and Decimal in my database. But a...
3
by: Boot2TheHead | last post by:
This one cost me a solid half hour yesterday. I'm wondering why on earth the default precision for a decimal type is 18,0. Maybe I'm mistaken. A decimal datatype sort of implies that you'd want...
23
by: neha_chhatre | last post by:
which is the best format specifier(data type) if i have to work with decimal number. also please tell me the syntax for truncating a decimal number please reply as soon as possible
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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...

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.