简体   繁体   English

如何在Visual Studio中将输出值设置为2个小数位?

[英]How can I set an output value to 2 decimal places in Visual Studio?

Im putting together a basic Gross Profit Calculator in Visual Studio and need the output to show as a £ value, ie to 2 decimal places. 我在Visual Studio中放了一个基本的毛利润计算器,需要将输出显示为£值,即小数点后两位。

I have tried this so far: 到目前为止,我已经尝试过了:

String.Format("{0:0.00}", TextBox3.Text = CStr(sale))

where TextBox3 is the output for the calculation. 其中,TextBox3是计算的输出。 However on using this, nothing happens and the box remains empty and I can't work out why! 但是,在使用此功能时,什么也没有发生,并且该框仍然为空,我无法弄清原因!

Your syntax is not correct, re-write your code this way: 您的语法不正确,请按照以下方式重新编写代码:

TextBox3.Text = String.Format("{0:0.00}", sale)

The String.Format 's result wasn't being assigned to anything. 没有将String.Format的结果分配给任何东西。

The syntax is wrong, and besides, CStr is just another way to format other types as a string. 语法是错误的,此外, CStr只是将其他类型格式化为字符串的另一种方法。 By using it you prevent String.Format from doing its job 通过使用它,可以防止 String.Format

I think you are looking for 我想你在找

TextBox3.Text = String.Format("{0:0.00}", sale) 

Furthermore, built-in numeric types override ToString and allow custom formatting as well. 此外,内置数字类型将覆盖ToString并允许自定义格式。 If sale is a float, double or decimal, you can write: 如果sale是浮点数,双精度数或十进制数,则可以编写:

TextBox3.Text = sale.ToString("0.00")

or 要么

TextBox3.Text = sale.ToString("N2")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在URL Visual Studio Basic .NET VB中转义句点/小数 - How can I escape a period/decimal inside URL Visual Studio Basic .NET VB 如何使用此可视化基本代码将列表框中的值四舍五入到小数点后两位 - How to I make the values in the list box round to two decimal places with this visual basic code 使用Visual Basic无法将具有小数点后12位的双精度/十进制值转换为字符串 - Unable to convert a double/decimal value with 12 decimal places to a string using visual basic 如何在Visual Studio中为自定义控件设置要编辑的默认事件? - How can I set the default event to be edited for my custom control in Visual Studio? 如何从 Visual Studio 应用程序打开文件资源管理器 window 并设置 position 和大小? - How can I open a File Explorer window from a Visual Studio app and set the position and size? 如何将 excel 格式设置为 0.00% 到 0.0000 十进制值 - How do I set excel format 0.00% to 0.0000 decimal value SQL - 如何四舍五入到小数点后两位 - SQL - how to round to 2 decimal places Visual Basic 如何在切换窗体时设置变量的值 - Visual Basic How can I set the value of an variable when switching forms 尽管四舍五入到两位小数,但仅输出一个小数位 - Only single decimal place output despite rounding to 2 decimal places Visual Studio - 如何将变量用于对象名称? - Visual Studio - How i can use a variable into a object name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM