从指数的符号显示更简单的方法prevent号 [英] Easier way to prevent numbers from showing in exponent notation

查看:182
本文介绍了从指数的符号显示更简单的方法prevent号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要依靠的说,毫无疑问是一个愚蠢的问题,但我有一个稍微愚蠢的一个要求。

I'm going to rely on the saying that no question is a dumb question, but I have a slightly dumb one to ask.

编辑:

似乎这个问题已经被问及这里已经回答了几次,但使用的标题为重复搜索时,我没遇到过。下面是一些相关的帖子:

Seems that this question has been asked and answered a few times on here already, though using titles I didn't come across when searching for duplicates. Here are some related posts:


  1. http://stackoverflow.com/questions/1546113/double-to-string-conversion-without-scientific-notation

  2. http://stackoverflow.com/questions/1319191/how-to-convert-double-to-string-without-the-power-to-10-re$p$psentation-e-05

  3. 有一个答案(乔恩斯基特)地点: HTTP://www.yoda.arachsys .COM / CSHARP / DoubleConverter.cs

  1. http://stackoverflow.com/questions/1546113/double-to-string-conversion-without-scientific-notation
  2. http://stackoverflow.com/questions/1319191/how-to-convert-double-to-string-without-the-power-to-10-representation-e-05
  3. Place with an answer (Jon Skeet): http://www.yoda.arachsys.com/csharp/DoubleConverter.cs


原题:

我有一个应用程序,有时会存储一些体面的小号码,如 0.000023425 。这些数字得到文本框用户编辑控制装起来。下面的人为的例子说明我的问题:

I've got an app that sometimes stores some decently small numbers, such as 0.000023425. These numbers get loaded up in TextBox controls for users to edit. The following contrived example shows my issue:

Dim number As Double = 0.000023425
someTextBox.Text = number.ToString() ' Shows "2.3425E-05"

如上所述,显示文字是 2.3425E-05 ,这是不准确的直观的用户看到,加上我有号码更precise(出19位小数)。我想输出是固定的点。当然,我可以很容易地和干净做的:

As mentioned, the text that shows is 2.3425E-05, which isn't exactly intuitive for the user to see, plus I have numbers even more precise (out to 19 decimal places). I would like the output to be fixed point. Of course I could easily and cleanly do:

number.ToString("F20") ' Shows "0.00002342500000000000"

不过,再有就是遗留零一个恼人的数量。因此,要解决这个问题,我可以这样做:

But then there's an annoying number of zeros left over. So, to fix that, I could do:

number.ToString("#,##0.####################") ' Shows "0.000023425"

这就是我想要的,但有没有更好的办法做到这一点,而不是有巨丑格式字符串呢?那么,好吧,它不是那么难看,但肯定有不同的方式,对不对?我的目标是展示被存储在数据库中以友好的方式的原始价值,我宁可不要强行格式的数量都没有。

Which is what I want, but is there any better way to do it than to have that giant ugly format string there? So, ok, it's not that ugly, but surely there's a different way, right? My goal is to show the raw value being stored in the DB in a friendly way, and I would rather not have to force a format on the number at all.

谢谢!

更新

也许我说在后有点过分。一些实验后,我发现,改变基础数据类型为小数解决了这个问题。

Perhaps I said a bit too much in the post. After some experimentation, I found that changing the underlying datatype to Decimal solves the issue.

Dim number As Decimal = 0.000023425
someTextBox.Text = number.ToString() ' Shows "0.000023425" instead of "2.3425E-05"

因此​​,它听起来就像双击是不是precise足以正常显示?

So it sounds like Doubles aren't precise enough to be displayed normally?

修改

<我一个href=\"http://stackoverflow.com/questions/1319191/how-to-convert-double-to-string-without-the-power-to-10-re$p$psentation-e-05/1546440#1546440\">found后(虽无upvotes),简单地蒙上就可以了的ToString()之前做的Double值作为一个小数,其中有预期的效果。是否有任何理由,我不想这样做呢?也许投地小数已结束与一个不同的值比双击(即使它是一个可以忽略不计的潜力),所以也许在#,## 0 #################。... 格式字符串是安全的。

I found a post (albeit with no upvotes) that simply casts the the Double value as a Decimal before doing a .ToString() on it, which has the desired effect. Is there any reason why I wouldn't want to do that? Perhaps the cast to Decimal has the potential to end with a different value than the Double (even though it's a negligible amount), so perhaps the #,##0.#################... format string is safer.

推荐答案

请您格式字符串常数(在你的资源文件的发言权),并通过名称使用它。你避免看到丑陋的格式,并获得在您的格式从控制控制的一致性(即,如果您改变了主意,所有控件将获得崭新的面貌)。

Make your format string a constant (say in your resource file) and use it by name. You avoid seeing the ugly format and gain consistency from control to control in your format (i.e., if you change your mind, all controls will gain the new look).

作为替代(或联合),获得从文本框自定义的控制和有调用你希望在一个更简单的语法使用不同格式字符串的属性。

As an alternative (or in conjunction), derive a custom control from the text box and have properties that invoke the different formatting strings you wish to use in an easier syntax.

无论哪种方式,你的目标应该是不重复自己......通过将配置在一个地方你避免最终键入错误,并有追查奇怪的格式错误。

Either way your goal should be not repeat yourself... by placing the configuration in one place you avoid mistyping it eventually and having to track down a strange formatting bug.

我们做创建百分比和封装所有的格式化和解析要求货币文本控件类似的东西。他们的工作就像文字,否则控制。

We do something similar by creating "percent" and "currency" text controls that encapsulate all the formatting and parsing requirements. They work just like text controls otherwise.

这篇关于从指数的符号显示更简单的方法prevent号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆