-1

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, Convert this line of Java code to C# code

C#

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

Java

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

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

6
  • Also, your format string is a bit off. The first number after '{' is a placeholder, starting with 0. After the ':' you place the formatting. Commented Sep 2, 2020 at 7:57
  • The error is telling you the format string is wrong. You have three '{}', so it should be '"{0:fromatstring} {1:formatstring} {2:format string}'. In your posted string, the second placeholder starts with 7, when you only have 3 total placeholders. Commented Sep 2, 2020 at 7:58
  • See learn.microsoft.com/en-us/dotnet/api/… and learn.microsoft.com/en-us/dotnet/standard/base-types/… for more info. Commented Sep 2, 2020 at 8:01
  • For example, (not that familiar with Java), the first placeholder could be something like {0:d}, and with String.Format would be String.Format("{0:d}", 6); Commented Sep 2, 2020 at 8:02
  • 1
    PLEASE. PLEASE. PLEASE. DO not close this. Perhaps show a Java minimal reproducible example with actual inputs and actual output so we can see what you are trying to achieve in C#? Commented Sep 2, 2020 at 8:12

2 Answers 2

2

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 :)
  • {1,7:f1} turns 20.5 into " 20.5" (3 leading spaces due to the 7 before the :)
  • {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.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Tim, %6d %7.1f %5.1f <-- how ? Because the way I understand it, is that those are replaced by other values in Java. Although I do not know what they actually mean.
I don't know much about Java, but I believe that's just Java's way of formatting strings. It'll take the 6 and format as a decimal, the 7.1 as a float, etc. The above code snippet will return "6 7.10 5.10" (because I didn't indicate the precision. What does the output in Java look like?
@Tim It's obvious what values will be generated based on variable data types. is what the OP said the last time we asked to see the output.
@mjwills I'm not familiar enough with Java's string formatting to say for certain (its also past my bedtime so maybe my brain shut off). The double/double thing confuses me, though I've been able to figure out %6d works with the length of the integer passed in. That's why I edited my answer to see it's a very basic intro to String.Format in C#.
0

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);

2 Comments

Welcome! and happy to help :)
This answer doesn't take into account the padding and precision in the Java formatting. May or may not be of importance to the OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.