将字符串格式化为货币 [英] Formatting A String As Currency

查看:68
本文介绍了将字符串格式化为货币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这让我感到疯狂......


我正在使用VS.NET 2003并尝试在
$ b中连续拍摄一个项目$ b松散类型的数据集并将其作为货币放在标签中。就像现在一样,

我得到的未格式化数据值(小数点)就好了,所以我知道

数据检索没有问题,只是格式化。


我读过这会起作用:


lblPrice.Text = prodRow [" Price"]。ToString( C);


但是,我收到错误,表明ToString没有任何带有任何参数的重载

方法。


我试过这些:


lblTotal.Text = String.Format(" {0:c}",prodRow [" Total"]) ;

lblTotal.Text = String.Format(" {0:c}",prodRow [" Total"]。ToString());

lblTotal。 Text = String.Format(" {0:c}",(decimal)prodRow [" Total"]);


,每个只返回数字,但没有格式化作为货币。


我怀疑问题是我从第一个p中的松散类型的
数据集中获取数据花边,因为我把这个测试代码放到了我的页面中,

它运行得很好:


双倍价格= 4.56;

lblTax .Text = String.Format(" {0:c}",price);


这是我的原始代码,它确实产生了我的数据(只是没有格式化):


DataRow prodRow = ds.Tables [0] .Rows [0];

lblPriceEach.Text = prodRow [" RetailPrice"]。ToString() ;

lblPriceAll.Text = prodRow [" PriceAll"]。ToString();

lblTax.Text = prodRow [" Tax"]。ToString();

lblTotal.Text = prodRow [" Total"]。ToString();


我甚至试过这个(将未格式化的数据放入标签):


lblTotal.Text = String.Format(" {0:c}",lblTotal.Text);


但是,这只是生成原始数字,没有任何格式。


我不做什么或做错了什么?!


谢谢

解决方案

您好Scott,


您能否验证prodRow [" Total"]返回数字数据类型,例如

系统.Decimal,但不是System.String?


格式化,0:C不能使用字符串Type。


-

Dave Sexton


" Scott M " < No **** @ NoSpam.com写信息

新闻:ef ************** @ TK2MSFTNGP02.phx.gbl ...


好​​的,这让我疯了......


我正在使用VS.NET 2003并试图取出一个项目

松散类型数据集中的一行,并将其作为货币放在标签中。就像现在一样,

我得到的未格式化数据值(小数点)就好了,所以我知道

数据检索没有问题,只是格式化。


我读过这会起作用:


lblPrice.Text = prodRow [" Price"]。ToString( C);


但是,我收到错误,表明ToString没有任何带有任何参数的重载

方法。


我试过这些:


lblTotal.Text = String.Format(" {0:c}",prodRow [" Total"]) ;

lblTotal.Text = String.Format(" {0:c}",prodRow [" Total"]。ToString());

lblTotal。 Text = String.Format(" {0:c}",(decimal)prodRow [" Total"]);


,每个只返回数字,但没有格式化作为一种货币。


我怀疑问题是我从一个松散类型的

数据集中获取数据首先是因为我把这个测试代码放到了我的页面中。

它工作得很好:


双倍价格= 4.56;

lblTax。 Text = String.Format(" {0:c}",price);


这是我的原始代码,它确实产生了我的数据(只是没有格式化):


DataRow prodRow = ds.Tables [0] .Rows [0];

lblPriceEach.Text = prodRow [" RetailPrice"]。ToString();

lblPriceAll.Text = prodRow [" PriceAll"]。ToString();

lblTax.Text = prodRow [" Tax"]。ToString();

lblTotal.Text = prodRow [" Total"]。ToString();


我甚至试过这个(将未格式化的数据放入标签后) ):


lblTotal.Text = String.Format(" {0:c}",lblTotal.Text);


但是,这只是生成原始数字,没有任何格式。


我不做什么或做错了什么?!


谢谢



嗨Dave,


prodRow [" Total"]返回一个对象(松散类型的数据集项)。


我怀疑是格式化不适用于String类型,所以我尝试了b $ b $(如发布):


lblTotal.Text = String.Format(" { 0:c}",(decimal)prodRow [" Total"]);


最后,我应该使用Convert类,它解决了我的问题>
问题。


谢谢,


Scott

" Dave Sexton" < dave @jwa [remove.this] online.comwrote in message

news:us ************* @ TK2MSFTNGP04.phx.gbl ...


您好Scott,


您能否验证prodRow [" Total]返回数字数据类型

作为System.Decimal,但不是System.String?


格式化,0:C不能使用字符串Type。


-

Dave Sexton


" Scott M " < No **** @ NoSpam.com写信息

新闻:ef ************** @ TK2MSFTNGP02.phx.gbl ...


>好的,这让我疯了......

我正在使用VS.NET 2003并试图连续拿一个项目在松散类型的数据集中,将其作为货币放在标签中。现在,我正在获取未格式化的数据值(小数点),所以
我知道数据检索没有问题,只是
格式化。

我读过这会起作用:

lblPrice.Text = prodRow [" Price"]。ToString(" C");

我试过这些:

lblTotal.Text = String.Format(" {0:c}",prodRow [" Total"]);
lblTotal.Text = String.Format(" {0:c}",prodRow [" Total" ;]。ToString());
lblTotal.Text = String.Format(" {0:c}",(decimal)prodRow [" Total"]);

每个只返回数字,但没有格式化为货币。

我怀疑问题是我从一个松散类型的数据集中获取数据,因为我丢弃了这个测试代码进入我的页面
并且它的工作正常:

双倍价格= 4.56;
lblTax.Text = String.Format(" {0:c}",price);

这是我的确实生成我的数据的原始代码(只是没有格式化):

DataRow prodRow = ds.Tables [0] .Rows [0];
lblPriceEach.Text = prodRow [" RetailPrice" ] .ToString();
lblPriceAll.Text = prodRow [" PriceAll"]。ToString();
lblTax.Text = prodRow [" Tax"]。ToString();
lblTotal.Text = prodRow [" Total"]。ToString();

我甚至试过这个(将未格式化的数据放入标签后):

lblTotal.Text = String.Format(" {0:c}",lblTotal.Text);

但是,这只是生成原始数字,没有任何格式。





嗨Scott,


prodRow [" Total"]返回一个对象(松散类型的数据集)项目)。



但是返回的对象可能是System.String,这就是我所要求的b
。您可以在调试器监视窗口中快速验证这一点。


我怀疑格式化不适用于String类型,所以我

尝试(已发布):


lblTotal.Text = String.Format(" {0:c}",(decimal)prodRow [" Total"]);



我怀疑会抛出InvalidCastException,因为prodRow [" Total"]是

返回System.String,而不是System.Decimal。


最后,我应该使用Convert类,它解决了我的

问题。



是的,以下内容应该有效:


lblTotal.Text = Convert.ToDecimal(prodRow [" Total]] ).ToString(" C");


FYI,您可能更喜欢使用强类型DataSet,以便可以使用以下代码

相反:


lblTotal.Text = prodRow.Total.ToString(" C");


HTH

-

Dave Sexton


" Scott M." < No **** @ NoSpam.com在留言中写道

新闻:uf ************** @ TK2MSFTNGP04.phx.gbl ...


嗨Dave,

prodRow [" Total"]返回一个对象(松散类型的数据集项)。


我怀疑格式化不适用于字符串类型,所以我尝试了b $ b(发布时):


lblTotal.Text = String.Format(" {0:c}",(decimal)prodRow [" Total"]);


最后,我应该是使用Convert类,解决了我的

问题。


谢谢,


Scott


" Dave Sexton" < dave @jwa [remove.this] online.comwrote in message

news:us ************* @ TK2MSFTNGP04.phx.gbl ...


>您好Scott,

您能否验证prodRow [" Total]返回的数值数据类型如
System.Decimal,但不是System.String?

格式化,0:C不会使用字符串Type。

-
Dave Sexton

Scott M. < No **** @ NoSpam.com写信息
新闻:ef ************** @ TK2MSFTNGP02.phx.gbl ...


>>好的,这让我疯了......

我正在使用VS.NET 2003并试图将一个项目排成一行
松散类型的数据集并将其作为货币放在标签中。现在,我正在获取未格式化的数据值(小数点),所以我知道数据检索没有问题,只是格式化。

我读过这会起作用:

lblPrice.Text = prodRow [" Price"]。ToString(" C");

但是,我收到错误,表明ToString没有任何可重载的方法。

我试过这些:

lblTotal.Text = String.Format( " {0:c}",prodRow [" Total"]);
lblTotal.Text = String.Format(" {0:c}",prodRow [" Total"]。ToString ());
lblTotal.Text = String.Format(" {0:c}",(decimal)prodRow [" Total"]);

并且每个都返回数字,但没有格式化为货币。

我怀疑问题是我首先从松散类型的数据集中获取数据,因为我将此测试代码放入我的页面
它的工作正常:

双倍价格= 4.56;
lblTax.Text = String.Format(" {0:c}",price);

这是我的原始代码确实产生我的数据(只是没有格式化):

DataRow prodRow = ds.Tables [0] .Rows [0];
lblPriceEach.Text = prodRow [" RetailPrice"]。ToString ();
lblPriceAll.Text = prodRow [" PriceAll"]。ToString();
lblTax.Text = prodRow [" Tax"]。ToString();
lblTotal.Text = prodRow [" Total"]。ToString();

我甚至试过这个(将未格式化的数据放入标签后):

lblTotal.Text = String.Format(" {0:c}",lblTotal.Text);

但是,这只是生成原始数字,没有任何格式。

什么我不做或做错了?!





Ok, this is driving me nuts...

I am using VS.NET 2003 and trying to take an item out of a row in a
loosely-typed dataset and place it in a label as a currency. As it is now,
I am getting my unformatted data values (as decimals) just fine, so I know
there''s not a problem with the data retrieval, just the formatting.

I have read that this would work:

lblPrice.Text = prodRow["Price"].ToString("C");

But, I get errors indicating that ToString does not have any overloadable
methods that take any arguments.

I have tried these:

lblTotal.Text = String.Format("{0:c}", prodRow["Total"]);
lblTotal.Text = String.Format("{0:c}", prodRow["Total"].ToString());
lblTotal.Text = String.Format("{0:c}", (decimal) prodRow["Total"]);

and each just returns the number, but not formatted as a currency.

I suspect the problem is that I am getting the data from a loosely-typed
dataset in the first place because I dropped this test code into my page and
it works just fine:

double price = 4.56;
lblTax.Text = String.Format("{0:c}", price);

Here''s my original code that does produce my data (just not formatted):

DataRow prodRow = ds.Tables[0].Rows[0];
lblPriceEach.Text = prodRow["RetailPrice"].ToString();
lblPriceAll.Text = prodRow["PriceAll"].ToString();
lblTax.Text = prodRow["Tax"].ToString();
lblTotal.Text = prodRow["Total"].ToString();

I''ve even tried this (after putting the unformatted data into the label):

lblTotal.Text = String.Format("{0:c}", lblTotal.Text);

But, this just produces the raw number, without any formatting.

What am I not doing or doing wrong?!

Thanks

解决方案

Hi Scott,

Can you verify that prodRow["Total"] is returning a numeric data type such as
System.Decimal, but not System.String?

The formatting, "0:C" won''t work with the string Type.

--
Dave Sexton

"Scott M." <No****@NoSpam.comwrote in message
news:ef**************@TK2MSFTNGP02.phx.gbl...

Ok, this is driving me nuts...

I am using VS.NET 2003 and trying to take an item out of a row in a
loosely-typed dataset and place it in a label as a currency. As it is now,
I am getting my unformatted data values (as decimals) just fine, so I know
there''s not a problem with the data retrieval, just the formatting.

I have read that this would work:

lblPrice.Text = prodRow["Price"].ToString("C");

But, I get errors indicating that ToString does not have any overloadable
methods that take any arguments.

I have tried these:

lblTotal.Text = String.Format("{0:c}", prodRow["Total"]);
lblTotal.Text = String.Format("{0:c}", prodRow["Total"].ToString());
lblTotal.Text = String.Format("{0:c}", (decimal) prodRow["Total"]);

and each just returns the number, but not formatted as a currency.

I suspect the problem is that I am getting the data from a loosely-typed
dataset in the first place because I dropped this test code into my page and
it works just fine:

double price = 4.56;
lblTax.Text = String.Format("{0:c}", price);

Here''s my original code that does produce my data (just not formatted):

DataRow prodRow = ds.Tables[0].Rows[0];
lblPriceEach.Text = prodRow["RetailPrice"].ToString();
lblPriceAll.Text = prodRow["PriceAll"].ToString();
lblTax.Text = prodRow["Tax"].ToString();
lblTotal.Text = prodRow["Total"].ToString();

I''ve even tried this (after putting the unformatted data into the label):

lblTotal.Text = String.Format("{0:c}", lblTotal.Text);

But, this just produces the raw number, without any formatting.

What am I not doing or doing wrong?!

Thanks



Hi Dave,

prodRow["Total"] returns an object (loosely typed dataset item).

I suspected that the formatting wouldn''t work with the String type, so I
tried (as posted):

lblTotal.Text = String.Format("{0:c}", (decimal) prodRow["Total"]);

In the end, I should have been using the Convert class, which solved my
problem.

Thanks,

Scott
"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:us*************@TK2MSFTNGP04.phx.gbl...

Hi Scott,

Can you verify that prodRow["Total"] is returning a numeric data type such
as System.Decimal, but not System.String?

The formatting, "0:C" won''t work with the string Type.

--
Dave Sexton

"Scott M." <No****@NoSpam.comwrote in message
news:ef**************@TK2MSFTNGP02.phx.gbl...

>Ok, this is driving me nuts...

I am using VS.NET 2003 and trying to take an item out of a row in a
loosely-typed dataset and place it in a label as a currency. As it is
now, I am getting my unformatted data values (as decimals) just fine, so
I know there''s not a problem with the data retrieval, just the
formatting.

I have read that this would work:

lblPrice.Text = prodRow["Price"].ToString("C");

But, I get errors indicating that ToString does not have any overloadable
methods that take any arguments.

I have tried these:

lblTotal.Text = String.Format("{0:c}", prodRow["Total"]);
lblTotal.Text = String.Format("{0:c}", prodRow["Total"].ToString());
lblTotal.Text = String.Format("{0:c}", (decimal) prodRow["Total"]);

and each just returns the number, but not formatted as a currency.

I suspect the problem is that I am getting the data from a loosely-typed
dataset in the first place because I dropped this test code into my page
and it works just fine:

double price = 4.56;
lblTax.Text = String.Format("{0:c}", price);

Here''s my original code that does produce my data (just not formatted):

DataRow prodRow = ds.Tables[0].Rows[0];
lblPriceEach.Text = prodRow["RetailPrice"].ToString();
lblPriceAll.Text = prodRow["PriceAll"].ToString();
lblTax.Text = prodRow["Tax"].ToString();
lblTotal.Text = prodRow["Total"].ToString();

I''ve even tried this (after putting the unformatted data into the label):

lblTotal.Text = String.Format("{0:c}", lblTotal.Text);

But, this just produces the raw number, without any formatting.

What am I not doing or doing wrong?!

Thanks




Hi Scott,

prodRow["Total"] returns an object (loosely typed dataset item).

But the object returned is probably System.String, and that''s what I was
asking. You can quickly verify this in a debugger watch window.

I suspected that the formatting wouldn''t work with the String type, so I
tried (as posted):

lblTotal.Text = String.Format("{0:c}", (decimal) prodRow["Total"]);

I suspect that will throw an InvalidCastException because prodRow["Total"] is
returning System.String, not System.Decimal.

In the end, I should have been using the Convert class, which solved my
problem.

Yes, the following should work:

lblTotal.Text = Convert.ToDecimal(prodRow["Total"]).ToString("C");

FYI, you might prefer to use a strong-typed DataSet so that the following code
could be used instead:

lblTotal.Text = prodRow.Total.ToString("C");

HTH

--
Dave Sexton

"Scott M." <No****@NoSpam.comwrote in message
news:uf**************@TK2MSFTNGP04.phx.gbl...

Hi Dave,

prodRow["Total"] returns an object (loosely typed dataset item).

I suspected that the formatting wouldn''t work with the String type, so I
tried (as posted):

lblTotal.Text = String.Format("{0:c}", (decimal) prodRow["Total"]);

In the end, I should have been using the Convert class, which solved my
problem.

Thanks,

Scott
"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:us*************@TK2MSFTNGP04.phx.gbl...

>Hi Scott,

Can you verify that prodRow["Total"] is returning a numeric data type such
as System.Decimal, but not System.String?

The formatting, "0:C" won''t work with the string Type.

--
Dave Sexton

"Scott M." <No****@NoSpam.comwrote in message
news:ef**************@TK2MSFTNGP02.phx.gbl...

>>Ok, this is driving me nuts...

I am using VS.NET 2003 and trying to take an item out of a row in a
loosely-typed dataset and place it in a label as a currency. As it is
now, I am getting my unformatted data values (as decimals) just fine, so I
know there''s not a problem with the data retrieval, just the formatting.

I have read that this would work:

lblPrice.Text = prodRow["Price"].ToString("C");

But, I get errors indicating that ToString does not have any overloadable
methods that take any arguments.

I have tried these:

lblTotal.Text = String.Format("{0:c}", prodRow["Total"]);
lblTotal.Text = String.Format("{0:c}", prodRow["Total"].ToString());
lblTotal.Text = String.Format("{0:c}", (decimal) prodRow["Total"]);

and each just returns the number, but not formatted as a currency.

I suspect the problem is that I am getting the data from a loosely-typed
dataset in the first place because I dropped this test code into my page
and it works just fine:

double price = 4.56;
lblTax.Text = String.Format("{0:c}", price);

Here''s my original code that does produce my data (just not formatted):

DataRow prodRow = ds.Tables[0].Rows[0];
lblPriceEach.Text = prodRow["RetailPrice"].ToString();
lblPriceAll.Text = prodRow["PriceAll"].ToString();
lblTax.Text = prodRow["Tax"].ToString();
lblTotal.Text = prodRow["Total"].ToString();

I''ve even tried this (after putting the unformatted data into the label):

lblTotal.Text = String.Format("{0:c}", lblTotal.Text);

But, this just produces the raw number, without any formatting.

What am I not doing or doing wrong?!

Thanks





这篇关于将字符串格式化为货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆