Java格式化字符串至C#

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

Java Format string to C#

问题

不理解Java的语法。如何将 "%6d %7.1f %5.1f" 转换为C#的等价写法?

我在C#中始终获得以下打印输出:%6d %7.1f %5.1f

尝试过:

$"{{0:d6}} {{7:1f}} {{5:1f}}"

但是,遇到了异常。

异常信息:

未处理的异常: System.FormatException: 索引(从零开始)必须大于等于零且小于参数列表的大小。
   在 System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args)
   在 System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args)
   在 System.String.Format(String format, Object arg0, Object arg1, Object arg2)
   在 experiment.Main(String[] args)

Java代码:

String.format(""%6d %7.1f %5.1f"", int, double, double/double);

根据变量数据类型,生成的值是显而易见的。

编辑:我刚刚查看了,https://stackoverflow.com/questions/17635617/convert-this-line-of-java-code-to-c-sharp-code

C#

String.Format(""{{0:x2}}"", arrayOfByte[i]);

Java

String.format(""%02x"", arrayOfByte[i]);

请。请。请。不要关闭这个。请求您了。

英文:

Don't understand Java's syntax. How to convert: "%6d %7.1f %5.1f" to C# equivalent ?

I keep getting this print out in C#: %6d %7.1f %5.1f

Tried:

"{0:d6} {7:1f} {5:1f}"

But, ran into an exception.

Exception:

Unhandled Exception: System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
   at System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args)
   at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args)
   at System.String.Format(String format, Object arg0, Object arg1, Object arg2)
   at experiment.Main(String[] args)

The Java code:

String.format("%6d %7.1f %5.1f", int, double, double/double);

It's obvious what values will be generated based on variable data types.

EDIT: I just looked at, https://stackoverflow.com/questions/17635617/convert-this-line-of-java-code-to-c-sharp-code

C#

String.Format("{0:x2}", arrayOfByte[i]);

Java

String.format("%02x", arrayOfByte[i]);

PLEASE. PLEASE. PLEASE. DO not close this. Kindly. Please.

答案1

得分: 2

注意:根据对Java格式说明符的(希望如此)更好理解,我完全重写了原来的回答。

根据我的(受限于Google的)理解,%6d%7.1f%5.1f对应以下内容:

  • 整数,最多6个字符,如果少于6个则填充。
  • 浮点数,最多7个字符(包括小数点和小数部分),精度为1。
  • 浮点数,最多5个字符(包括小数点和小数部分),精度为1。

你可以使用C#的String.Format来实现,像这样:

var newString = String.Format("{0,6:d} {1,7:f1}, {2,5:f1}", 605, 20.5, 8.22);

这将得到以下字符串:

"   605    20.5  8.22"

花括号({})内的第一个数字对应于字符串之后传入的参数:

  • 0 = 605
  • 1 = 20.5
  • 2 = 8.22

逗号后的第二个数字表示字符串的长度(包括小数点和小数部分)。

  • 6 = 整数的6个字符
  • 7 = 浮点数的7个字符
  • 5 = 浮点数的5个字符

冒号后的字母和数字是格式说明符。

  • d = 整数
  • f1 = 浮点数,精度为1。

这将生成如上所示的字符串:

  • {0,6:d} 将605转换为" 605"(在冒号之前的6导致3个前导空格)
  • {1,7:f1} 将20.5转换为" 20.5"(在冒号之前的7导致3个前导空格)
  • {2,5:f1} 将8.22转换为" 8.2"(在冒号之前的5导致1个前导空格,精度导致1个小数数字)。

正如我之前所说,查阅String.FormatStandard Numeric Format Strings以获取更多信息。

英文:

NOTE: Completely rewrote my original answer based on a (hopefully) better understanding of the Java format specifiers.

Based on my (Google-limited understanding), %6d, %7.1f and %5.1f correspond to the following:

  • An integer with up to 6 characters, padded if less than 6.
  • A float with up to 7 characters (including the decimal point and decimal portion) with a precision of 1.
  • A float with up to 5 characters (including the decimal point and decimal portion) with a precision of 1.

You can accomplish this with C#'s String.Format, like this:

var newString = String.Format("{0,6:d} {1,7:f1}, {2,5:f1}", 605, 20.5, 8.22);

This will result in the following string:

"   605    20.5  8.22"

The first digit in each placeholder group (defined by { and }) corresponds to the argument passed in after the string:

  • 0 = 605
  • 1 = 20.5
  • 2 = 8.22

The second digit, after the , refers to the length of the string (including decimal points and decimal portions).

  • 6 = 6 characters for the integer
  • 7 = 7 characters for the float
  • 5 = 5 characters for the float

The letters and numbers after the : are the format specifiers.

  • d = integer
  • f1 = floating with a precision of 1.

Which produces the string above, as follows:

  • {0,6:d} turns 605 into " 605" (3 leading spaces due to the 6 before the Java格式化字符串至C#
  • {1,7:f1} turns 20.5 into " 20.5" (3 leading spaces due to the 7 before the Java格式化字符串至C#
  • {2,5:f1} turns 8.22 into " 8.2" (1 leading space due to the 5 before the : and 1 decimal number due to the precision).

As I said earlier, check String.Format and Standard Numeric Format Strings for more information.

答案2

得分: 0

从C# 6开始,你可以使用插值。对于你的情况,你可能想尝试以下内容:

string formattedString = $"{0:d6} {7.1:f} {5.1:f}";

在C# 6之前,你可以尝试以下内容:

string formattedString = String.Format("{0:d6} {1:f} {2:f}", 0, 7.1, 5.1);
英文:

Starting from C# 6. you can use interpolation.
For your case you may wanted to try the following:

string formattedString = $"{0:d6} {7.1:f} {5.1:f}";

before C# 6 you can try the following:

string formattedString = String.Format("{0:d6} {1:f} {2:f}", 0, 7.1, 5.1);

huangapple
  • 本文由 发表于 2020年9月2日 15:44:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63700919.html
匿名

发表评论

匿名网友

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

确定