decimal to string conversion in C#
How can I convert a list of decimal values to strings such that:
- No decimal point is shown if the value is an integer
- Otherwise, the number is formatted to a minimum of two decimal places
This should result in the following string values:
3 Answers 3
You can use the decimal.ToString override to specify a formatting.
This can also be used when using String.Format.
In the case of your first and second rule, it cannot be done on one line.
The following extension method should satisfy you requirements. The comments on the code was provided in OP and comments.
![]()
You can write a method to get you the number of decimal places in the number like this:
Then you can use Fx specifier where x is the number of decimal places like this:
Localization
If you care about localization, then use the code below so it will work for any culture since some cultures use , as decimal:
There are various formats as shown on MSDN. Here is some code copy pasted from there:
Convert decimal to string
I wrote a C# program that converts decimal to string . It is not the best but it works. I’m looking for inputs from others on how can it be improved? One aspect that baffled me was how to find out total number of digits after decimal point? Right now I’ve to pass it as a separate parameter. It is in C#, but I’m open to any programming language. credit goes to all authors that I found online and little bit to me
![]()
4 Answers 4
You don’t really need to know the length of the fractional part. Simply separate the fractional from the integral and handle each one separately.
EDIT added check for negative
If you want to take the integral and fractional parts of a decimal , use Decimal.Truncate(Decimal) instead of a cast to int : Decimal can represent much much larger numbers than int , and you code will presently fail for such large numbers.
Simply dividing by 10 won’t work anymore, because of course decimal will keep the fraction part; rather, you can subtract the last ‘digit’ before dividing (see code below).
Note that everyone has changed the name of i from your code: i screams ‘counter’; integralPart and such are much better names. d having a sensible name is even more important, because it is part of the API exposed by the method. Good naming is really important.
Your method will always leave a «.» after the integral part, even if there is no fractional part, except for the special case of 0 . This seems inconsistent, and may or may not be a concern. (it’s not what Decimal.ToString() does, for instance).
You’ve not asked about performance, but I’ll just point out that inserting at the start of a string-builder is more expensive than adding to the end (linear vs. amortised constant). This means you code is actually quadratic in the string-length of the value. You could make it linear by writing the integral part backwards, and then reversing it, but who cares.
Code based on tinstaalf’s code, but using decimal integral and fractional parts (should hopefully work for any decimal value):
Как decimal перевести в string c
Decimal.ToString() Method is used to convert the numeric value of the current instance to its equivalent string representation using the specified culture-specific format information. There are 4 methods in the overload list of this method as follows:
- ToString() Method
- ToString(IFormatProvider) Method
- ToString(String, IFormatProvider) Method
- ToString(String) Method
Here, we will discuss the first two methods.
ToString() Method
This method is used to convert the numeric value of the current instance to its equivalent string representation.
Syntax: public override string ToString ();
Return Value: This method returns a string which represents the value of the current instance.
Below programs illustrate the use of Decimal.ToString() Method:
Example 1:
Example 2:
ToString(String) Method
This method is used to convert the numeric value of the current instance to its equivalent string representation, using the specified format.
Syntax: public string ToString (string format);
Here, it takes a standard or custom numeric format string.Return Value: This method returns the string representation of the value of the current instance as specified by format.
Exceptions: This method throws FormatException if format is invalid.
How to convert a decimal value to a string in C#
Get Educative’s definitive System Design Interview Handbook for free.
Overview
Decimal.ToString() is a method in C# that is called on decimal values. It converts a numeric value that is a decimal to a string.
Syntax
Parameters
dec : This is the decimal value that we want to convert to a string.
Return value
The value returned is a string representation of dec .
Code example
Explanation
Here is a line-by-line explanation of the above code:
Lines 10-12: We create variables d1 , d2 , and d3 of type decimal and assign decimal values to them.
Lines 16-26: We convert each value to a string using the ToString() method. Then we print the converted values and their type using the GetType() method.
As we can see in the output, the decimal values are converted to strings.