Currency,Decimal,Access and Confusion

  • Thread starter Thread starter MikeY
  • Start date Start date
M

MikeY

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(myDataReader["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
 
MikeY said:
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(myDataReader["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
 
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 said:
MikeY said:
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(myDataReader["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
 
I do believe the problem lies in the data not coming across from the Access.

MikeY said:
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 said:
MikeY said:
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(myDataReader["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
 
MikeY said:
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(myDataReader["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.
 
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 said:
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(myDataReader["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 said:
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.
 
MikeY said:
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.
 
Thanks again Jon for the response and the confirmation. I'll just stick with
the way I have it then.

MikeY
 
Thanks again Jon, I'll give it a try.

MikeY

Jon Skeet said:
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.)


Yes, that's probably the easiest way of "forcing" it to a certain
number of decimal places.
 
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(myDataReader["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
 
Hi Otis, Thanks for the response I'll give it a go and see how your syntax
works out.

Thanks again

MikeY

Otis Mukinfus said:
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(myDataReader["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
 
Back
Top