string number format

  • Thread starter Thread starter LW Irving
  • Start date Start date
L

LW Irving

when I use the snippet below

price = "$" + reader["start_price"].ToString() + " to $" +
reader["end_price"].ToString();

the price is returned with 4 decimal places

I thought I could do; reader["start_price"].ToString("C") to format the
string as currency and do away "$" + from the line

or reader["start_price"].ToString("00.00") to at least specify the decimal
places

but niether work

What am I doing wrong



Regards

Wayne
 
I also tried
price = "$" + Convert.ToDecimal( reader["start_price"]).ToString("0.00") + "
to $" + Convert.ToDecimal( reader["end_price"]).ToString("C");

I tried tostring("C2") as well

which compiled at least but did not make any difference to the output ie
still four decimal places
 
When you try
price = Convert.ToDecimal( reader["end_price"]).ToString("C");

what do you get as price?

José

LW Irving said:
I also tried
price = "$" + Convert.ToDecimal( reader["start_price"]).ToString("0.00") + "
to $" + Convert.ToDecimal( reader["end_price"]).ToString("C");

I tried tostring("C2") as well

which compiled at least but did not make any difference to the output ie
still four decimal places

LW Irving said:
when I use the snippet below

price = "$" + reader["start_price"].ToString() + " to $" +
reader["end_price"].ToString();

the price is returned with 4 decimal places

I thought I could do; reader["start_price"].ToString("C") to format the
string as currency and do away "$" + from the line

or reader["start_price"].ToString("00.00") to at least specify the decimal
places

but niether work

What am I doing wrong



Regards

Wayne
 
LW Irving said:
when I use the snippet below

price = "$" + reader["start_price"].ToString() + " to $" +
reader["end_price"].ToString();

the price is returned with 4 decimal places

I thought I could do; reader["start_price"].ToString("C") to format the
string as currency and do away "$" + from the line

or reader["start_price"].ToString("00.00") to at least specify the decimal
places

but niether work

What am I doing wrong

Firstly, we need to know what reader["start_price"] is returning. Then
we need to know what you mean by "but neither work" - in what *way*
don't they work?
 
djozy said:
Please,can you tell me,witch function converts integer to
string?

Convert.ToString, or Int32.ToString, or String.Format would all be good
starting points.
 
As far as I know (Convert.ToDecimal(Value)).ToString("0.00") should work
because it come from the IFormatter interface and is the same one used for
Float's. Double's etc...

I have used this many times... oh and there is just one thing you shoudl
undestand about the IFormatter if you do the following
(Convert.ToDecimal(Value)).ToString("0.00") it will round up to the nearest
2 it doesnt just chop off the rest... which is cool

Mitch
 
Thanks for the replies
I was mistaken (ie. I was altering a method that was not being called
therefore there was no observable change)
The start_price variable is a sql variable in the data base it is decared as
smallmoney, it seems to behave as if double.

These two statements work
price = "$" + Convert.ToDecimal(reader["start_price"]).ToString("0.00")
or
price = Convert.ToDecimal(reader["start_price"]).ToString("C",
NumberFormatInfo.CurrentInfo)
With the second I do not need to put in the dollar sign

I need to do this about 120 - 130 times to fix the code ( I did not write it
originally and am just learning c# as I go)

these methods are on pages a in a folder called modules on pages called
along the lines of S3generalDB.cs

So my next question is can I do something like

price = myprice

and have a method that says

myprice = Convert.ToDecimal(reader["start_price"]).ToString("C",
NumberFormatInfo.CurrentInfo)

if should I put it at the top of the page , and how should I declare it

Sorry if this is too simple but I am really new to .net and C#

Regards

Wayne







LW Irving said:
when I use the snippet below

price = "$" + reader["start_price"].ToString() + " to $" +
reader["end_price"].ToString();

the price is returned with 4 decimal places

I thought I could do; reader["start_price"].ToString("C") to format the
string as currency and do away "$" + from the line

or reader["start_price"].ToString("00.00") to at least specify the decimal
places

but niether work

What am I doing wrong



Regards

Wayne
 
Well because C# is Component/Object Orientated (Where as C is Procedural
i.e. declare first then use etc...)
Yes Something like this:
decimal myprice = 0.0M;
decimal price = myprice;

myprice = Convert.ToDecimal(reader["start_price"]).ToString("C",
NumberFormatInfo.CurrentInfo);

: will work becuase C# doesnt look at the lines and say erf oops here is
something wrong, to get a better grasp of how C#/.NET works I recommend
familiarise yourself with MSIL the underlying stuff that makes .NET,.NET...
for instance if you look at the code above in MSIL (compile the view exe or
dll in ILDASM) you will notice that you are doing quite a bit of boxing and
unboxing. (Casting from Reference Type to Value Type and Vice Versa) Try
this.... SQL types are said to be more accurate...

decimal myprice = 0.0M;

string price = myprice.ToString("C", NumberFormatInfo.CurrentInfo)
// If you call price here now you will get $0.00
myprice = (decimal)myReader.GetSqlMoney(0);
// but if you call it here (lets say the DB returns 10.0000) you will get
$10.00 does this clear things up ??

Mitch

LW Irving said:
Thanks for the replies
I was mistaken (ie. I was altering a method that was not being called
therefore there was no observable change)
The start_price variable is a sql variable in the data base it is decared as
smallmoney, it seems to behave as if double.

These two statements work
price = "$" + Convert.ToDecimal(reader["start_price"]).ToString("0.00")
or
price = Convert.ToDecimal(reader["start_price"]).ToString("C",
NumberFormatInfo.CurrentInfo)
With the second I do not need to put in the dollar sign

I need to do this about 120 - 130 times to fix the code ( I did not write it
originally and am just learning c# as I go)

these methods are on pages a in a folder called modules on pages called
along the lines of S3generalDB.cs

So my next question is can I do something like

price = myprice

and have a method that says

myprice = Convert.ToDecimal(reader["start_price"]).ToString("C",
NumberFormatInfo.CurrentInfo)

if should I put it at the top of the page , and how should I declare it

Sorry if this is too simple but I am really new to .net and C#

Regards

Wayne







LW Irving said:
when I use the snippet below

price = "$" + reader["start_price"].ToString() + " to $" +
reader["end_price"].ToString();

the price is returned with 4 decimal places

I thought I could do; reader["start_price"].ToString("C") to format the
string as currency and do away "$" + from the line

or reader["start_price"].ToString("00.00") to at least specify the decimal
places

but niether work

What am I doing wrong



Regards

Wayne
 
Back
Top