0

I currently display 3 numbers like this

1 567,90

1 234,00

1 235,00

Using the following line of code

<b class='dash-main-val'> {String.Format("{0:N}", info.Rows[rowCountX][columnNames[0]])

I want to keep the spacing when it reaches 1000s, but I do not want to display the ,00 if there's no decimal. Is there a way of having the decimal be optional and dynamic as possible as it is coming from a Datatable?

  • Have you tried splitting the string using decimal, then if array length is 1 it would mean you don't have decimal? – Mausam Jun 26 '20 at 21:25
  • You might need to clarify how your data is stored in the datatable (ie, if the stored numbers without decimals end with ",00" or not). –  Jun 26 '20 at 21:36
  • see: https://stackoverflow.com/questions/4525854/remove-trailing-zeros – Jonathan Jun 27 '20 at 06:38

1 Answers1

1

AFAIK there is not way to customize the "N"-Formatter. But, if you know how large your numbers will go try this:

edit: you will need to append a .Trim(), otherwise you will have leading spaces...

123456789.ToString("### ### ###.##").Trim()
// "123 456 789"

(123456789.55).ToString("### ### ###.##").Trim()
// "123 456 789.55"

// your code:
<b class='dash-main-val'> {String.Format("{0:### ### ###.##}", info.Rows[rowCountX][columnNames[0]]).Trim()}
halliba
  • 309
  • 2
  • 8