简体   繁体   中英

Issue with String.Format output

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.

设置页面上字段的代码

文本控件的标记

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?

页面上呈现的控件

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM