0

I am currently trying to display at most 2 decimal places in a smallcurrency field that is being pulled from a SQL database. As you can see in the image below I am using format specifiers to limit the number of decimal places that will be rendered to the controls on the screen.

Code that sets the fields on the page

The markup for the text controls

However as you can see in the image below the fields are displaying up to 4 decimal places (despite the database containing the records only holding up to 2 decimal places). I have attempted a variety of different format specifiers as well as omitting the String.Format completely however this has had no effect.

Am I missing something obvious here or is something strange happening with my work?

The controls rendered on the page

1 Answer 1

2

You shouldn't call ToString() on the value inside Format. That counteracts the format completely, turning the value into a string (with 4 decimal places in your case), and ignores all other formating.

If you want to leave the textbox blank (as per your comments) when the field is empty, surround the assignment with an if statement.

If ProductInformation.Rows(0)("MonthlyPrice") IsNot DBNull.Value Then
    Me.txtMonthlyPrice.Text = String.Format("{0:0.##}", ProductInformation.Rows(0)("MonthlyPrice"))
Else
    Me.txtMonthlyPrice.Text = ""
End If
Sign up to request clarification or add additional context in comments.

2 Comments

the reason I am calling toString() is to handle the case where the fields might be empty in the DB, is there an easy way to keep this functionality without messing up the String.Format call?
What would you like to happen if the field is empty? keep the textbox clear? (Does your field allow null values at all?)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.