Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

java printf

// printf formats the text by placing the placeholders into the first string
// placeholders are noted first with '%s' and then you list them in their order
System.out.printf( "Test ( %s ): %s", 3.75, "ok" );
// will give: 
Test ( 3.75 ): ok
// comlete placeholders you use for known variables:
// %f : float and double
// %d : int and long
// %c : char
// %b : boolean
float f = 15.47f;
long l = 765432L;
char c = '@';
boolean b = true;
System.out.printf( "Test2 ( %f ): %d -> %c%b", f, l, c, b );
// will give: 
Test2 ( 15,470000 ): 765432 -> @true
// number of decimals:
// %.4f : 4 decimals after the dot
System.out.printf( "Test3 ( %.4f ): %d -> %c%b", f, l, c, b );
// will give:
Test3 ( 15,4700 ): 765432 -> @true
 
PREVIOUS NEXT
Tagged: #java #printf
ADD COMMENT
Topic
Name
5+3 =