c# - 用字符串格式伪造小数
问题描述
我目前在一个小型 C# 程序中有以下多头。
long one = 1;
long two = 1005;
long three = 100000005;
long four = 1111112258552;
我想将它们格式化为字符串,使它们的大小为千分之一,而不用将值除以一千。
例子
Input : Output
1 0.001
1005 1.005
100000005 100000.005
1111112258552 1111112258.552
我已经尝试过字符串格式{0:0,000}
,{0:0.000}
但都没有提供我想要的结果。
我怎样才能达到我所追求的结果?任何提示或指示将不胜感激
一些示例代码
long one = 1;
long two = 1005;
long three = 100000005;
long four = 1111112258552;
string format = "{0:0,000}";
string s1 = String.Format(format, one);
string s2 = String.Format(format, two);
string s3 = String.Format(format, three);
string s4 = String.Format(format, four);
Console.WriteLine(s1);
Console.WriteLine(s2);
Console.WriteLine(s3);
Console.WriteLine(s4);
解决方案
为您的格式尝试“{0:0,.000}”。
long one = 1;
long two = 1005;
long three = 100000005;
long four = 1111112258552;
string format = "{0:0,.000}";
string s1 = String.Format(format, one);
string s2 = String.Format(format, two);
string s3 = String.Format(format, three);
string s4 = String.Format(format, four);
Console.WriteLine(s1);
Console.WriteLine(s2);
Console.WriteLine(s3);
Console.WriteLine(s4);
控制台输出:
0.001
1.005
100000.005
1111112258.552
推荐阅读
- google-apps-script - 使用 Google API 将标题(用于选定文本)从 Google Doc 插入到 Google Sheets
- c - 运行分配静态内存的简单程序时出现 DEADLYSIGNAL 错误
- c# - 注入 ILogger
- android - 预填充的 Android Room 数据库保持为空
- flutter - 为什么 ElevatedButton 被拉伸以适应屏幕的宽度?
- javascript - 按下输入文本字段时增加 div 元素的 paddingTop,否则减少
- ios - 在 react-native run-ios 上构建失败
- python - 虚拟环境如何在 python 中工作(内部)
- android - Jetpack Compose 中的垂直嵌套滚动
- php - 通过字符串路径获取数组的值