0

I already know HOW to format number string with comma like I need from this thread, in fact, I am trying to apply the accepted answer to my case.

However, from what I see, it deals with predetermined string variable. In my case, it is running a for-loop, which converts a double value to string and displays output as table rows.

for (double i = 1; i <= years; i++)
{
                              //number of years         //future value
   richTextBoxResults.Text += i.ToString().PadLeft(3) + (presentValue * Math.Pow(((interestRate / 100 / periods) + 1), (periods * i))).ToString("$#.00").PadLeft(28) + "\n";
}

To keep things simple, 3 output rows should be sufficient to get the idea

Years                                Future Value
__________________________________________________
  1                                    $1234567.89
  2                                    $2345678.90
  3                                    $3456789.01

I tried to use Format() inside the loop after the .ToString("$#.00") method, however, I was getting an error of Method() cannot be accessed _with an instance reference. However, I am not sure how I can apply those answers to my case.

I was thinking about creating a string variable, which would temporarily store values and format it. But I am wondering if there is a more elegant solution.

Can I still apply Format() method (maybe under the different angle), so my output would be like the following

Years                                Future Value
__________________________________________________
  1                                  $1,234,567.89
  2                                  $2,345,678.90
  3                                  $3,456,789.01

Or to I need to change my approach to that?

Community
  • 1
  • 1
Vadzim Savenok
  • 930
  • 3
  • 14
  • 37

3 Answers3

3

Your code is not readable which is making it hard for you to understand the problem. You have an extra ) before the .ToString :

Math.Pow(((interestRate / 100 / periods) + 1), (periods * i)).ToString("$#.00").PadLeft(28)

You can use "$0,#.00" to apply a thousands separator.

Zein Makki
  • 29,485
  • 6
  • 52
  • 63
  • Yeah, I tried to make my line more readable, but switching it to a new line made it more confusing, so I decided to leave it as is. I also applied your bottom solution, and it worked! I will accept as soon as limit runs out. – Vadzim Savenok Aug 10 '16 at 18:21
  • @VadzimSavenok You would regret reading this line of code you posted after 2 months from now. It would take more time to understand then. I highly suggest that you split the calculations into multiple lines. Readability, Readability then Readability. Not just for others reading your code, but for you to be able to maintain and modify it at later stages if necessary. – Zein Makki Aug 10 '16 at 18:24
  • I will certainly apply it now, it's just that I had a word wrap on, so it was quite readable to me. But I see what you mean. – Vadzim Savenok Aug 10 '16 at 18:27
1

To make your code readable, put your data inside a variable. and then format it like this.

 var _value = presentValue * Math.Pow(((interestRate / 100 / periods) + 1), (periods * i));
 richTextBoxResults.Text+= i.ToString().PadLeft(3) + String.Format("{0:0.00}",_value) .PadLeft(28) ;
Ashkan S
  • 10,464
  • 6
  • 51
  • 80
0

You would have to call Format() as a Static method of the string class :

string.Format(/*YourParameters*/)
Noémie Lord
  • 751
  • 4
  • 22