Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
532 views
in Technique[技术] by (71.8m points)

c# - How to change display format of long variable?

I have a variable of type Long i.e.

long quantity=1000;

I want to display it like 1,000 in Grid (Must need commas)

How do i achieve this?

I am using a Telerik Grid and I am binding the data as follows:

columns.Bound(tempProductList => tempProductList.tempProductListQuantity) .Title("Quantity")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here you have a list of all the standard numeric formats. I think "N" is the one you want.

long l = 1234;
string s  = l.ToString("N0"); //gives "1,234"

The "0" after the format specifier is the number of desired decimal places (usually 2 by default).

Note that this version is culture-sensitive, i.e., in my country, we use dots (".") as thousand separators, so the actual returned value will be "1.234" instead of the "1,234". If this is desired behaviour, just leave it as is, but if you need to use commas always, then you should specify a culture as a parameter to the ToString method, like

l.ToString("N0", CultureInfo.InvariantCulture); //always return "1,234"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...