2

I have a bug in my website when display salary.

If number insert < 1000$. It will show result like 0,x00.

Example:

user inserts to admin page salary: 800$. It will show like 0,800$.

If salary > 1000$, it shows correct.

My function to GetSalary() like:

public string GetSalary(object SalaryFrom, object SalaryTo)
{
    return "$" + Protector.Int(SalaryFrom).ToString("0,000") + " - " + Protector.Int(SalaryTo).ToString("0,000");
}
Ave
  • 4,338
  • 4
  • 40
  • 67

4 Answers4

2

Use ToString() by this way:

.ToString("#,000")
Roman
  • 11,966
  • 10
  • 38
  • 47
2

You can use the following format string to display a number with a maximum of 2 decimal places:

String.Format("{0:0.##}", 123.4567);      // "123.46"

See Using String Format to show decimal upto 2 places or simple integer

Community
  • 1
  • 1
Georg Patscheider
  • 9,357
  • 1
  • 26
  • 36
2

0 in a format string means "absolutely show this digit". It sounds like you want something like this instead:

ToString("#,##0")

# means "a digit that might not be present".

Of course, this will still only show the thousands separator for thousands, and not millions. Another option might be using one of the pre-defined number formats - for example ToString("c") which will use the current culture's currency format (e.g. £1,234.00 for United Kingdom).

Luaan
  • 62,244
  • 7
  • 97
  • 116
1

Try this code:

public string GetSalary(object SalaryFrom, object SalaryTo)
{
   return "$" + @String.Format("{0:N}", SalaryFrom.ToString()) + " - " + @String.Format("{0:N}", SalaryTo.ToString());
}
imsome1
  • 1,182
  • 4
  • 22
  • 37