String.Format 获取“2.4k”从“2400”开始
有没有办法使用 String.Format 将数字转换为数字/字符表示形式。
例如
2400 -> 2.4k
2,600,000 -> 2.6m
Is there a way to use String.Format to convert numbers to number/character representations.
For example
2400 -> 2.4k
2,600,000 -> 2.6m
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我提供示例代码。请尝试按照你的方式去做
I am providing a sample code. Please try to make it in your way
不,我不相信有一个格式字符串可以为您做到这一点。
您可能会找到第三方库来完成此操作,并且我知道有内置的 Win32 例程可以将文件大小转换为类似的表示形式,但这很可能使用 1024 而不是 1000 K/M等的“基地”。 这个 Stack Overflow 答案 显示了一些 C# 代码,但我确信有一些东西在平台中也是如此......正如我所说,这是针对文件大小的,这可能是也可能不是您想要的。
No, I don't believe there's a format string that will do that for you.
You may well find third party libraries to do it, and I know there are built-in Win32 routines to convert a file size to a representation like that, but that may well use 1024 rather than 1000 for the "base" of the K/M/etc. This Stack Overflow answer shows some C# code for it, but I'm sure there's something in the platform too... and as I say, that's aimed at file sizes, which may or may not be what you want.
我为你写了这个。
I wrote this for you.
您还可以尝试以下操作:
将您的数字转换为科学格式的字符串:2400 -> 2.4e+003; 2,6000,000 -> 2,6000,000 2.6e+006
然后用所需的 SI 前缀替换指数(例如 e+003 -> k、e+006 -> M、e-009 -> n)
为此目的,我创建了以下扩展:
You can also try the following:
Convert your number into a string in scientific format: 2400 -> 2.4e+003; 2,6000,000 -> 2.6e+006
Then replace the exponent with the desired SI-prefix (e.g. e+003 -> k, e+006 -> M, e-009 -> n)
I've created the following extension for this purpose:
您将无法使用 String.Format 执行此操作,但您可以尝试以下操作:
You won't be able to do this using String.Format but you can try this:
我认为你的问题很普遍,无论如何我为你发布了一些案例的简单答案:
I think your question is general, anyway I post you a simple answer for some case:
这是扩展形式的正确形式。
this is correct one in extention form.
功能:
The function:
这是一个单行代码,类似于:
产生:
注意:它使用
System.Interactive
作为Scan
操作符。Here's a one-liner, sort of:
That produces:
NB: It uses
System.Interactive
for theScan
operator.