获取双精度数后面的字符

发布于 2024-10-06 19:30:33 字数 481 浏览 1 评论 0原文

我觉得这是一个非常菜鸟的问题..但我只是无法得到正确的说法。

出于显示目的,我想将 double 分成两部分:点之前的部分和点之后的前两位数字。我需要它作为字符串。目标语言:C#。

例如: 2345.1234 变为 "2345""12"

我知道如何获取点之前的部分,这很简单:

Math.Floor(value).ToString()

...但是获得“点后面”部分的正确方法是什么? 一定有一些好的方法可以以简单的方式做到这一点......

那么我想不出其他什么了:

Math.Round(100 * (value - Math.Floor(value))).ToString("00");

我确信有更好的方法,但我就是想不到。有人吗?

I feel like this is a very noob question.. but I just can't get the right statement for it.

For display purposes, I want to split a double in two: the part before the dot and the first two digits after the dot. I need it as a string. Target language: C#.

E.g.: 2345.1234 becomes "2345" and "12"

I know how to get the part before the dot, that's simply:

Math.Floor(value).ToString()

...but what is the right way to get the part "behind the dot"?
There must be some nice way to do that in a simple way...

I can't think of anything else then:

Math.Round(100 * (value - Math.Floor(value))).ToString("00");

I'm sure there is a better way, but I just can't think of it. Anyone?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不离久伴 2024-10-13 19:30:33

正则表达式 (regex) 可能是您最好的选择,但使用 mod 运算符可能是另一个有价值的解决方案......

  stuffToTheRight = value % 1

干杯。

Regular expressions (regex) is probably you best bet, but using the mod operator may be another valuable solution...

  stuffToTheRight = value % 1

Cheers.

小矜持 2024-10-13 19:30:33
//
//Use the Fixed point formatting option.  You might have a bit more work to do 
//if you need to handle cases where "dot" is not the decimal separator.
//
string s = value.ToString("F2", CultureInfo.InvariantCulture);
var values = s.Split(".");
string v1 = values[0];
string v2 = values[1];

有关格式设置的更多信息,请参阅此链接:http://msdn.microsoft.com/en -us/library/dwhawy9k.aspx

这是一些未经测试的代码,试图考虑当前的文化:

//
//Use the Fixed point formatting option.
//
string s = value.ToString("F2", CultureInfo.CurrentCulture);
var values = s.Split(CultureInfo.NumberFormat.NumberDecimalSeparator);
string v1 = values[0];
string v2 = values[1];
//
//Use the Fixed point formatting option.  You might have a bit more work to do 
//if you need to handle cases where "dot" is not the decimal separator.
//
string s = value.ToString("F2", CultureInfo.InvariantCulture);
var values = s.Split(".");
string v1 = values[0];
string v2 = values[1];

See this link for more about formatting: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

Here is some untested code that tries to take current culture into account:

//
//Use the Fixed point formatting option.
//
string s = value.ToString("F2", CultureInfo.CurrentCulture);
var values = s.Split(CultureInfo.NumberFormat.NumberDecimalSeparator);
string v1 = values[0];
string v2 = values[1];
多孤肩上扛 2024-10-13 19:30:33
use regex ".[0-9][0-9]"
use regex ".[0-9][0-9]"
爱冒险 2024-10-13 19:30:33

在一行中它将是:
string[] vals = value.ToString("f2").Split(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator.ToCharArray());

vals[0] :点之前.
vals[1] :点之后。

In one line it will be:
string[] vals = value.ToString("f2").Split(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator.ToCharArray());

vals[0] : before point.
vals[1] : after point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文