-4

I am trying to display the original value in C# three part display format. I got unexpected result + 29291221321. I am expecting + 29.12121321. Is this a bug or what am I doing wrong?

double value1 = 29.1221321;
string formattingString = $"+ {value1}; - {value1}; 0";
 // returns "+29.1221321; -29.1221321; 0"
Console.WriteLine(value1.ToString(formattingString));
// returns "+ 29291221321" 

Please refer to why i call ToString at the end. It is something known as three part format to separate outcome quickly based on +, - and 0 value https://diptimayapatra.wordpress.com/2014/01/13/3-part-format-of-numbers-in-c/

Note:
I am not expecting to hardcode by specify x numbers of # after decimal places. Please advise if there is a better method of displaying the original decimal value in three part format

 string formattingString = "+ #.############; - #.############; 0";
17
  • 4
    You are putting the value itself into the formatting string, instead of placeholders. It's unclear what kind of formatting string you even want. Commented Mar 9, 2021 at 14:49
  • that is quite strange that the semicolon is doing that: learn.microsoft.com/en-us/dotnet/csharp/language-reference/… Commented Mar 9, 2021 at 14:49
  • 1
    formattingString isn't a format string, it's the output of the string interpolation operations performed by $"+ {value1}; - {value1}; 0";. The second format string works just fine Commented Mar 9, 2021 at 14:54
  • 4
    Presumably you want to output formattingString directly Console.WriteLine(formattingString); Commented Mar 9, 2021 at 14:54
  • 1
    @eulercode Your question does not make sense. You have achieved the result that you believe is correct (+ 29.1221321; - 29.1221321; 0), why are you trying to further pass that result to ToString() if it's already correct? It is not a formatting string in the first place. Commented Mar 9, 2021 at 14:58

3 Answers 3

4

Please advise if there is a better method of displaying the original decimal value in three part format

# is already the best way to express what you want, since it will omit non-significant zeros at the end. A double has about 15 to 17 significant digits, so you can put 17 # at the end of your format specifier to get the original value.

double value1 = 1.23456;
string formattingString = "+ #.#################; - #.#################; 0";
Console.WriteLine(value1.ToString(formattingString));
Console.WriteLine((-value1).ToString(formattingString));
Console.WriteLine(0.ToString(formattingString));

Output (note the leading spaces, because you have spaces in your format specifier):

+ 1,23456
 - 1,23456
 0

Check that for a double value with more digits

double value1 = 1.2345678901234567890123;   // too many digits for a double

and compare it to the output of

Console.WriteLine(value1);
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, I accept your answer on this. But i have create a new post that requires further explanation here: stackoverflow.com/questions/66549716/…. Hope u can comment there as well why putting . inside will cause unpredictable output
0

You're putting the value into the format specifier instead of your specifier:

string formattingString = $"+ {value1}; - {value1}; 0";

Your snippet should instead look like this:

double value1 = 29.1221321;
string formattingString = $"+ #.############; - #.############; 0";
Console.WriteLine(value1.ToString(formattingString));

With the added understanding that you do not want to use the hard coded number of decimal places (which is understandable given the precision available with double), I'm not aware of a way using interpolation. I'd recommend a simple extension method for it:

public static string ToSignedString(this double value) {
    if (value < 0)
        return $"-{value}";
    else if (value > 0)
        return $"+{value}";
    
    return "0";
}

I don't typically recommend extension methods, but I believe your reasoning for not wanting hard coded decimal places is perhaps the need for the code to be concise due to heavy use.

double value = 29.1221321;
Console.WriteLine(value.ToSignedString());

Not the most graceful solution, but it would work.

2 Comments

Hi i have mentioned i do not want to use second solution given with hardcoded "#" after decimal places
@eulercode Depending on what are you intending to calculate the required number of #?
-1

You try to reformat Value1 another time in the Console.WriteLine Change it to :

Console.WriteLine(formattingString);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.