Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Round decimal for display only when the decimal point is 0 (eg: 3.00 --> 3)

I am storing Amount in Decimal(18,2). But most of my data is actually integer with only few decimal. So when I display the amount for each item in the table, the number display is all 3.00, 4.00, 5.00 and so on.

I feel it is kind of weird to display all the 0 as the decimal point. I wish to format the number when display if the decimal point is 0.

Currently I am displaying the amount like this:

  <td id="amnt@(item.FoodID)" class="amountfield">
      @Html.DisplayFor(modelItem => item.FoodAmount)
  </td>

Any idea?? Appreciate every help... thanks...

like image 662
shennyL Avatar asked Dec 13 '11 10:12

shennyL


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Labbell LabNokia Bell Labs, originally named Bell Telephone Laboratories (1925–1984), then AT&T Bell Laboratories (1984–1996) and Bell Labs Innovations (1996–2007), is an American industrial research and scientific development company owned by multinational company Nokia.https://en.wikipedia.org › wiki › Bell_LabsBell Labs - Wikipedia. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

Looking at your code I assume that you are using ASP.Net MVC for your application. You can use DataAnnotation for formatting your data.

You can try using DisplayFormat in your model property.

[DisplayFormat(DataFormatString = "{0:0.##}")] 
Decimal FoodAmount{ get; set; }
like image 152
Maheep Avatar answered Nov 03 '22 04:11

Maheep


Use ToString(string) with a custom format using the "#" custom format specifier

item.FoodAmount.ToString("0.##")

This shows up to two digits but omits them if they are non-significant zeroes.

like image 35
Stefan Paul Noack Avatar answered Nov 03 '22 05:11

Stefan Paul Noack