How to use String.format in Java

1 Answer

0 votes
public class Program
{
	public static void main(String[] args)
	{
		// Common Format Specifiers:
        // %c -	Unicode character
        // %d - Integer value (byte, short, int, long, bigint)
        // %s - String value
        // %f - Decimal value (floating point)
        // %x - Hextstring value from integer (%X)

        char ch = 'z';
        int n = 3728;
        String s = "java";
        float f = 3.14f;
        double d = 24891.572;
        int hex = 255;

		String str_formated = String.format("ch = %c n = %d s = %s %.2f %.3f %x %X", ch, n, s, f, d, hex, hex); 

		System.out.println(str_formated);
	}
}



/*
run:

ch = z n = 3728 s = java 3.14 24891.572 ff FF
  
*/

 



answered Jun 26, 2024 by avibootz
edited Jun 26, 2024 by avibootz

Related questions

1 answer 163 views
163 views asked Mar 25, 2021 by avibootz
5 answers 339 views
1 answer 193 views
3 answers 228 views
228 views asked Oct 9, 2020 by avibootz
1 answer 198 views
2 answers 205 views
1 answer 105 views
...