在vb.net中将小数点限制为4位数字

huangapple go评论71阅读模式
英文:

Limiting the decimal point into 4 digits in vb.net

问题

我可以在声明变量为十进制时限制小数点,但在声明为字符串时无法限制小数点。因为我需要它在字符串数据类型中。

^^ 上面的代码有效

但这个不行:

Dim result As String
result = ComputationResult
Textbox1.Text = result.ToString("N4")
英文:

I can limit the decimal point when I declare the variable as decimal though I can't when I declare it as a string. Because I need it in string datatype.

Dim result As Decimal
result = ComputationResult
Textbox1.Text = result.ToString("N4")

^^ The above code works

But this doesn't :

Dim result As String
result = ComputationResult
Textbox1.Text = result.ToString("N4")

答案1

得分: 1

如果ComputationResult是一个Decimal,当result声明为字符串时,您在此行上使用默认格式将Decimal隐式转换为String

 result = ComputationResult

所以现在result已经是一个字符串,您失去了使用数字格式字符串的机会。 ToString() 调用中的 "N4" 没有意义:

 Textbox1.Text = result.ToString("N4")

它试图使用 这个重载,其中 "N4" 将无法匹配任何格式提供程序... 但这无关紧要,因为链接的文档说这个参数 被忽略

> provider is reserved, and does not currently participate in this operation.
>
> Because this method simply returns the current string unchanged, there is no need to call it directly.

另一方面,如果 ComputationResult 是一个 String,那么当 result 是一个 Decimal 时,会强制将 String 隐式转换为 Decimal,这允许 result.ToString("N4") 调用使用了解格式字符串的 这个重载


从中我们学到的是 数据类型很重要! 更好的开发人员会意识到在表达式的每个级别都使用的数据类型,并知道在适当的地方使用适当的类型,同时避免不必要的转换。

请记住,由于文化/国际化问题,在数字(或日期)和字符串之间进行转换比您想象的要慢得多,而且更容易出错。这不是我们希望的快速或简单的操作,而是要避免的事情。通常的最佳计划是尽可能长时间地将值保持为数字类型,只有在最后可能的时刻才将其转换为字符串进行输出。

实际上,result = ComputationResult 分配对于这两个示例都可以编译,这告诉我您已经关闭了 Option StrictOption Infer 中的至少一个,这是非常糟糕的! 您应该打开其中至少一个,然后纠正程序中出现的错误,直到代码再次编译。这将使您的代码更快,崩溃的可能性更小,一旦适应了这个限制,您实际上会成为一个更快的开发人员。


由于我们不知道 ComputationResult 的实际类型,代码有两个可能的正确版本。如果它是一个字符串,您可以选择以下方式:

Textbox1.Text = Decimal.Parse(ComputationResult).ToString("N4")

或者如果它是一个 Decimal:

Textbox1.Text = ComputationResult.ToString("N4")

实际上,如果 ComputationResult 被合理定义,它应该已经是一个 Decimal

最后,看起来 ComputationResult 可能 是一个没有参数的方法。虽然 VB.Net 仍然支持在参数列表中不使用括号来调用方法,但不应该这样做。对于 .Net,请始终在调用方法时使用括号。

Textbox1.Text = ComputationResult().ToString("N4")

但如果 ComputationResult 不是一个方法,您可以忽略此最后一项。

英文:

If ComputationResult is a Decimal, when result is declared as a string you have an implicit conversion from Decimal to String on this line using the default format:

 result = ComputationResult

So now result is already a string, and you lost the opportunity to use numeric format strings. The "N4" in the ToString() call has no meaning:

 Textbox1.Text = result.ToString("N4")

It's trying to use this overload, where N4 will fail to match any format provider... but it doesn't matter, because the linked documentation says this argument is ignored anyway:

> provider is reserved, and does not currently participate in this operation.
>
> Because this method simply returns the current string unchanged, there is no need to call it directly.

On the other hand, if ComputationResult is a String, the version where result is a Decimal forces an implicit conversion from String to Decimal, which allows allows the result.ToString("N4") call to use this overload that knows about format strings.


What we learn from this is data types matter! Better developers are aware of the data type in play at every level of their expressions, and know how to use the appropriate types in the appropriate places, while avoiding needless conversions.

Remember, thanks to cultural/internationalization issues, converting between a number (or date) and a string is FAR slower and more error-prone than you expect. It's not the quick or simple operation we'd like it to be, but rather something to avoid. The best plan is generally to keep a value in a number type for as long as possible, and only convert to string for output at the last possible moment.

Really, the fact the result = ComputationResult assignment compiles for both samples tells me you have both Option Strict and Option Infer turned off, and that's REALLY BAD! You should turn at least one of those on, and then go correct the resulting errors in your program until the code compiles again. You will be SO MUCH BETTER OFF for this work: your code will be faster, MUCH less likely to crash, and it will actually make you a faster developer once you learn to adapt to the constraint.


Since we don't know that actual type of ComputationResult, there are two possible correct versions of the code. Either you want this if it's a String:

Textbox1.Text = Decimal.Parse(ComputationResult).ToString("N4")

or this if it's a Decimal:

Textbox1.Text = ComputationResult.ToString("N4")

And really, if ComputationResult is defined in a sane way it will already be a Decimal.

Finally, it looks like ComputationResult might be a method with no arguments. While VB.Net still supports calling methods without parentheses for the argument list, you should not do that. For .Net, ALWAYS use parentheses when calling your methods.

Textbox1.Text = ComputationResult().ToString("N4")

But if it's not the case ComputationResult is a method you can ignore this last item.

答案2

得分: 0

如果您的 cumputationResult 已经是字符串,请使用 Decimal.Parse 将其转换为十进制,然后对其进行数字格式化。

Dim cumputationResult = "1.234567"
Textbox1.Text = Decimal.Parse(cumputationResult).ToString("N4")
英文:

If your cumputationResult is already a string, convert it to a decimal using Decimal.Parse, then do the number formatting on it.

Dim cumputationResult = "1.234567"
Textbox1.Text = Decimal.Parse(cumputationResult).ToString("N4")

huangapple
  • 本文由 发表于 2023年6月9日 00:04:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定