Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Decimal to string for currency

Tags:

To display a currency we do:

ToString("0.##") 

For value 5.00 the output is:

5 

For value 5.98 the output is:

5.98 

For value 5.90 the output is:

5.9 

I need the third case to come out with 2 decimal points, eg:

5.90 

How can I do this without it affecting the other results?

like image 337
Tom Gullen Avatar asked May 03 '12 18:05

Tom Gullen


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 ...

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 C and C++ meaning?

C is a function driven language because C is a procedural programming language. C++ is an object driven language because it is an object oriented programming. Function and operator overloading is not supported in C. Function and operator overloading is supported by C++. C is a function-driven language.


1 Answers

Try:

value.ToString("#,##0.00") 

Or just:

value.ToString("C") 

I know of no built-in way to expand out all two decimal places only when both are not zero. Would probably just use an if statement for that.

if (s.EndsWith(".00"))     s = s.Substring(0, s.Length - 3); 
like image 120
Jonathan Wood Avatar answered Sep 21 '22 13:09

Jonathan Wood