469,957 Members | 2,537 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,957 developers. It's quick & easy.

Currency,Decimal,Access and Confusion

Hi Everyone,

I'm trying to extract my pricing from my Access.mdb. Developing WinForm C#.
I've read previous posts on the subject, but I'm still confused.

In Access my "Price" is of "Currency" type. I extract my items
"myDataItem.MyPrice(Convert.ToDecimal(myDataRe ader["Price"]));

Now my problem like so many others is that if my dollar value is $10.00, and
when I try to extract my data, it converts some how to just "10". How do I
loose the ".00" decimal and the cents? If I have $10.99 value, all is
displayed correctly. Why does this do that, and what do I need to do to
correct this problem. As stated in past posts do I just need to take the
"10" and build a string.Format to rebuild/add the .00 decimal and cents?

Any and all help is appreciated,

MikeY
Apr 13 '06 #1
11 2772
MikeY wrote:
I'm trying to extract my pricing from my Access.mdb. Developing WinForm C#.
I've read previous posts on the subject, but I'm still confused.

In Access my "Price" is of "Currency" type. I extract my items
"myDataItem.MyPrice(Convert.ToDecimal(myDataRe ader["Price"]));

Now my problem like so many others is that if my dollar value is $10.00, and
when I try to extract my data, it converts some how to just "10". How do I
loose the ".00" decimal and the cents? If I have $10.99 value, all is
displayed correctly. Why does this do that, and what do I need to do to
correct this problem. As stated in past posts do I just need to take the
"10" and build a string.Format to rebuild/add the .00 decimal and cents?


There are three potential problems:
1) The precision of the data isn't coming across from the database
2) Convert.ToDecimal is being problematic
3) The way you're displaying it is problematic.

If you write:

Console.WriteLine (myDataReader["Price"].GetType());
Console.WriteLine (myDataReader["Price"].ToString());

what happens?

Jon

Apr 13 '06 #2
Hiya Jon,

I'm getting System.Decimal for the GetType();
I'm getting the Item price value as is should be. But for the "10.00" value
it is being shown as "10" (not correct), while the "9.95" is being displayed
as "9.95" (correct).

txs agian Jon,

MikeY
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
MikeY wrote:
I'm trying to extract my pricing from my Access.mdb. Developing WinForm
C#.
I've read previous posts on the subject, but I'm still confused.

In Access my "Price" is of "Currency" type. I extract my items
"myDataItem.MyPrice(Convert.ToDecimal(myDataRe ader["Price"]));

Now my problem like so many others is that if my dollar value is $10.00,
and
when I try to extract my data, it converts some how to just "10". How do
I
loose the ".00" decimal and the cents? If I have $10.99 value, all is
displayed correctly. Why does this do that, and what do I need to do to
correct this problem. As stated in past posts do I just need to take the
"10" and build a string.Format to rebuild/add the .00 decimal and cents?


There are three potential problems:
1) The precision of the data isn't coming across from the database
2) Convert.ToDecimal is being problematic
3) The way you're displaying it is problematic.

If you write:

Console.WriteLine (myDataReader["Price"].GetType());
Console.WriteLine (myDataReader["Price"].ToString());

what happens?

Jon

Apr 13 '06 #3
I do believe the problem lies in the data not coming across from the Access.

"MikeY" <mi*******@yaho.com> wrote in message
news:_B*****************@news20.bellglobal.com...
Hiya Jon,

I'm getting System.Decimal for the GetType();
I'm getting the Item price value as is should be. But for the "10.00"
value it is being shown as "10" (not correct), while the "9.95" is being
displayed as "9.95" (correct).

txs agian Jon,

MikeY
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
MikeY wrote:
I'm trying to extract my pricing from my Access.mdb. Developing WinForm
C#.
I've read previous posts on the subject, but I'm still confused.

In Access my "Price" is of "Currency" type. I extract my items
"myDataItem.MyPrice(Convert.ToDecimal(myDataRe ader["Price"]));

Now my problem like so many others is that if my dollar value is $10.00,
and
when I try to extract my data, it converts some how to just "10". How do
I
loose the ".00" decimal and the cents? If I have $10.99 value, all is
displayed correctly. Why does this do that, and what do I need to do to
correct this problem. As stated in past posts do I just need to take the
"10" and build a string.Format to rebuild/add the .00 decimal and cents?


There are three potential problems:
1) The precision of the data isn't coming across from the database
2) Convert.ToDecimal is being problematic
3) The way you're displaying it is problematic.

If you write:

Console.WriteLine (myDataReader["Price"].GetType());
Console.WriteLine (myDataReader["Price"].ToString());

what happens?

Jon


Apr 13 '06 #4

MikeY wrote:
Hi Everyone,

I'm trying to extract my pricing from my Access.mdb. Developing WinForm C#.
I've read previous posts on the subject, but I'm still confused.

In Access my "Price" is of "Currency" type. I extract my items
"myDataItem.MyPrice(Convert.ToDecimal(myDataRe ader["Price"]));

Now my problem like so many others is that if my dollar value is $10.00, and
when I try to extract my data, it converts some how to just "10". How do I
loose the ".00" decimal and the cents? If I have $10.99 value, all is
displayed correctly. Why does this do that, and what do I need to do to
correct this problem. As stated in past posts do I just need to take the
"10" and build a string.Format to rebuild/add the .00 decimal and cents?


The *number* 10 is the same however many zero digit decimal places you
append. The place you need to look (and show us if you don't see the
problem) is where you *display* the value.

Decimal.ToString doesn't automatically know how you want your ten
displayed - it doesn't know it represents a currency value; it's just
ten. ToString has overloads that take formatting information - this is
a good place to start digging around the docs.

--
Larry Lard
Replies to group please

Apr 13 '06 #5
Hi Larry,

At present, I am extracting my Currency into a decimal value. From there I
do format the the value to be displayed in my ListView. My code is as
follows:

private void ctrl_Bottom1_my_Test(string myName, decimal myPrice)
{
string unitPrice = System.String.Format ("{0:C}", myPrice);

this.lvBody.BeginUpdate();
this.lvBody..Items.Add(myName);
this.lvBody.Items[ListViewCounter].SubItems.Add(unitPrice.ToString());
this.lvBody.EndUpdate();

ListViewCounter += 1;
}

This way does work, But I wonder why I am loosing my .00 cents (of the
10.00) from the database when non-zero (9.95) characters are good. And if
there is a better way.

So gather what your wrote is that the only way to get the lots .00 is to
append the zeros back in. Like I am doing with the code above. Hmmmm

Thanks Larry,

MikeY

"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...

MikeY wrote:
Hi Everyone,

I'm trying to extract my pricing from my Access.mdb. Developing WinForm
C#.
I've read previous posts on the subject, but I'm still confused.

In Access my "Price" is of "Currency" type. I extract my items
"myDataItem.MyPrice(Convert.ToDecimal(myDataRe ader["Price"]));

Now my problem like so many others is that if my dollar value is $10.00,
and
when I try to extract my data, it converts some how to just "10". How do
I
loose the ".00" decimal and the cents? If I have $10.99 value, all is
displayed correctly. Why does this do that, and what do I need to do to
correct this problem. As stated in past posts do I just need to take the
"10" and build a string.Format to rebuild/add the .00 decimal and cents?


The *number* 10 is the same however many zero digit decimal places you
append. The place you need to look (and show us if you don't see the
problem) is where you *display* the value.

Decimal.ToString doesn't automatically know how you want your ten
displayed - it doesn't know it represents a currency value; it's just
ten. ToString has overloads that take formatting information - this is
a good place to start digging around the docs.

--
Larry Lard
Replies to group please

Apr 13 '06 #6
Larry Lard <la*******@hotmail.com> wrote:
Decimal.ToString doesn't automatically know how you want your ten
displayed - it doesn't know it represents a currency value; it's just
ten.
No, that's not true. Decimal (unlike, say, float and double) *does*
maintain the number of decimal places it considers itself to represent.
So 10, 10.0 and 10.00 are differently represented. Try the following:

using System;

class Test
{
static void Main()
{
decimal d1 = 10m;
decimal d2 = 10.0m;
decimal d3 = 10.00m;
Console.WriteLine ("{0} {1} {2}", d1, d2, d3);
}
}

(Try the equivalent with floats and you'll get a different answer.)
ToString has overloads that take formatting information - this is
a good place to start digging around the docs.


Yes, that's probably the easiest way of "forcing" it to a certain
number of decimal places.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 13 '06 #7
MikeY <mi*******@yaho.com> wrote:
I do believe the problem lies in the data not coming across from the Access.


Right. In that case, I believe the best solution is to format the
number as you want it when you turn it into text.

There's no "obvious" way of forcing the decimal itself to 2 decimal
places. In a couple of tests, adding 1.00m and then subtracting it
again works, but I wouldn't like to swear that it's guaranteed to - and
it's the kind of code which is likely to get removed by a maintenance
programmer as it looks like a no-op. Choosing an appropriate format
string is clearer, IMO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 13 '06 #8
Thanks again Jon for the response and the confirmation. I'll just stick with
the way I have it then.

MikeY

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
MikeY <mi*******@yaho.com> wrote:
I do believe the problem lies in the data not coming across from the
Access.


Right. In that case, I believe the best solution is to format the
number as you want it when you turn it into text.

There's no "obvious" way of forcing the decimal itself to 2 decimal
places. In a couple of tests, adding 1.00m and then subtracting it
again works, but I wouldn't like to swear that it's guaranteed to - and
it's the kind of code which is likely to get removed by a maintenance
programmer as it looks like a no-op. Choosing an appropriate format
string is clearer, IMO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Apr 13 '06 #9
Thanks again Jon, I'll give it a try.

MikeY

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Larry Lard <la*******@hotmail.com> wrote:
Decimal.ToString doesn't automatically know how you want your ten
displayed - it doesn't know it represents a currency value; it's just
ten.


No, that's not true. Decimal (unlike, say, float and double) *does*
maintain the number of decimal places it considers itself to represent.
So 10, 10.0 and 10.00 are differently represented. Try the following:

using System;

class Test
{
static void Main()
{
decimal d1 = 10m;
decimal d2 = 10.0m;
decimal d3 = 10.00m;
Console.WriteLine ("{0} {1} {2}", d1, d2, d3);
}
}

(Try the equivalent with floats and you'll get a different answer.)
ToString has overloads that take formatting information - this is
a good place to start digging around the docs.


Yes, that's probably the easiest way of "forcing" it to a certain
number of decimal places.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Apr 13 '06 #10
On Thu, 13 Apr 2006 11:22:11 -0400, "MikeY" <mi*******@yaho.com> wrote:
Hi Everyone,

I'm trying to extract my pricing from my Access.mdb. Developing WinForm C#.
I've read previous posts on the subject, but I'm still confused.

In Access my "Price" is of "Currency" type. I extract my items
"myDataItem.MyPrice(Convert.ToDecimal(myDataReade r["Price"]));

Now my problem like so many others is that if my dollar value is $10.00, and
when I try to extract my data, it converts some how to just "10". How do I
loose the ".00" decimal and the cents? If I have $10.99 value, all is
displayed correctly. Why does this do that, and what do I need to do to
correct this problem. As stated in past posts do I just need to take the
"10" and build a string.Format to rebuild/add the .00 decimal and cents?

Any and all help is appreciated,

MikeY


moneyString = string.Format("${0:0.00}", yourDecdimalNumber);

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Apr 14 '06 #11
Hi Otis, Thanks for the response I'll give it a go and see how your syntax
works out.

Thanks again

MikeY

"Otis Mukinfus" <ph***@emailaddress.com> wrote in message
news:2g********************************@4ax.com...
On Thu, 13 Apr 2006 11:22:11 -0400, "MikeY" <mi*******@yaho.com> wrote:
Hi Everyone,

I'm trying to extract my pricing from my Access.mdb. Developing WinForm
C#.
I've read previous posts on the subject, but I'm still confused.

In Access my "Price" is of "Currency" type. I extract my items
"myDataItem.MyPrice(Convert.ToDecimal(myDataRead er["Price"]));

Now my problem like so many others is that if my dollar value is $10.00,
and
when I try to extract my data, it converts some how to just "10". How do I
loose the ".00" decimal and the cents? If I have $10.99 value, all is
displayed correctly. Why does this do that, and what do I need to do to
correct this problem. As stated in past posts do I just need to take the
"10" and build a string.Format to rebuild/add the .00 decimal and cents?

Any and all help is appreciated,

MikeY


moneyString = string.Format("${0:0.00}", yourDecdimalNumber);

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com

Apr 14 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Gerry Abbott | last post: by
4 posts views Thread by digitalavatar | last post: by
3 posts views Thread by Dave Stone | last post: by
3 posts views Thread by Mark | last post: by
14 posts views Thread by Scott M. | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.