Found 34494 Articles for Programming

The # format flag in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

265 Views

Apply the # flag to the %o, %x, %e, and %f format specifiers. If you want to display the hexadecimal number with a 0x prefix, then precede the %x specifier with #.Preceding the %x specifier with a #, the hexadecimal number will be printed with a 0x prefix.Let us see an example wherein $e is used. It includes a decimal point, even if the decimal digits are not present −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); System.out.println(f.format("%#e", 5F)); } }Output5.000000e+00

Add grouping specifiers for large numbers in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

162 Views

For using the Formatter class, import the following package −import java.util.Formatter;We can group specifiers as shown below −Formatter f = new Formatter(); f.format("%,.2f", 38178.9889);The above sets thousands separator and 2 decimal places.The following is an example −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); f.format("%d", 50); System.out.println(f); f = new Formatter(); f.format("%,.2f", 38178.9889); System.out.println(f); } }Output50 38,178.99

Formatting a Negative Number Output with Parentheses in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

490 Views

A negative number output can be shown using the Formatter object −Formatter f = new Formatter(); f.format("%12.2f", -7.598); System.out.println(f);Try the below given code to format a Negative Number Output with Parentheses −Formatter f = new Formatter(); f.format("%(d", -50); System.out.println(f);The following is an example −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); f.format("% d", 50); System.out.println(f); // negative number inside parentheses f = new Formatter(); f.format("%(d", -50); System.out.println(f); } }Output50 (50)

Space format specifiers in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

1K+ Views

For format specifier, import the following package −import java.util.Formatter;Create a formatter object and set space format specifier −Formatter f = new Formatter(); f.format("% d", -50); System.out.println(f); f = new Formatter(); f.format("% d", 90); System.out.println(f); f = new Formatter(); f.format("%10d", -50); System.out.println(f); f = new Formatter(); f.format("% 10d", 90); System.out.println(f);The following is an example displaying different forms of space format specifiers −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); f.format("% d", -50); ... Read More

Left justify output in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

2K+ Views

Including a minus sign after the %, makes it left justified.Note − By default, output is right justifiedFirstly, create a formatter object −Formatter f = new Formatter();Now, use the format() method to left justify output −f.format("|%-15.5f|", 299.675796)The following is an example −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); // left justify f = new Formatter(); System.out.println(f.format("|%-15.5f|", 299.675796)); } }Output|299.67580 |

Vertically align numeric values in Java using Formatter

Samual Sam
Updated on 30-Jul-2019 22:30:24

397 Views

To vertically align numeric values in Java, use Formatter. For working with Formatter class, import the following package.import java.util.Formatter;Take an array −double arr[] = { 2.5, 4.8, 5.7, 6.5, 9.4, 8.4, 9.5, 10.2, 11.5 };While displaying this double array values, use the %f to set spaces −for (double d : arr) { f.format("%12.2f %12.2f %12.2f", d, Math.ceil(d), Math.floor(d)); }Above, we have also set the decimal places i.e. 12.2f is for 2 decimal places.The following is an example −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String[] argv) throws Exception { ... Read More

Floating-point hexadecimal in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

351 Views

For Floating-point hexadecimal form, use the %a format specifier.For Formatter, import the following package −import java.util.Formatter;Now create a Formatter object like this −Formatter f = new Formatter();Use the format() method for floating-point hexadecimal −f.format("%a", 298.45)The following is an example −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); // Floating-point hexadecimal form System.out.println(f.format("%a", 298.45)); } }Output0x1.2a73333333333p8

Format Specifier for Hash Code in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

158 Views

Format specifier for Hash Code us %h.Firstly, create a new object for Formatter and Calendar −Formatter f = new Formatter(); Calendar c = Calendar.getInstance();For hash code −f.format("%h", c)The following is an example that finds the hash code −Example Live Demoimport java.util.Calendar; import java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); Calendar c = Calendar.getInstance(); System.out.println("Hash Code: "+f.format("%h", c)); } }OutputHash Code: 4b899958

Formatter Specifier for Octal and Hexadecimal in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

152 Views

For Formatter, import the following package −import java.util.Formatter;Now create a Formatter object like this −Formatter f1 = new Formatter(); Formatter f2 = new Formatter(); Formatter f3 = new Formatter();If you want a Format specifier for Octal, use %o −f3.format("Octal values %o %o %o", 15, 55, 78);If you want a Format specifier for Hexadecimal, use %x −f2.format("Hexadecimal values %x %x %x", 24, 98, 110);The following is an example −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f1 = new Formatter(); ... Read More

Format Output with Formatter in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

235 Views

For Formatter, import the following package −import java.util.Formatter;Now create a Formatter object −Formatter f1 = new Formatter();Now, we will format the output with Formatter −f1.format("Rank and Percentage of %s = %d %f", "John", 2, 98.5);The following is an example −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f1 = new Formatter(); Formatter f2 = new Formatter(); f1.format("Rank and Percentage of %s = %d %f", "John", 2, 98.5); ... Read More

Advertisements