Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

What happened to printf or how do I format the numbers I need to print?

0
Posted

What happened to printf or how do I format the numbers I need to print?

0

Location: http://www.jguru.com/faq/view.jsp?EID=8882 Created: Jan 26, 2000 Modified: 2000-01-26 01:22:50.759 Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7) The Java libraries and println() method provide no direct equivalent to printf/sprintf. While Acme labs provides an alternative at http://www.acme.com/java/software/Acme.Fmt.html, the NumberFormat class provides a mechanism for setting the number of characters to display before and after the decimal point. For instance, the following would print out a number with commas for the thousand, millions, billions, etc, at least one number after the decimal point, and at most two numbers (w/o rounding) after the decimal point. NumberFormat format = new DecimalFormat(“#,##0.0#”); System.out.println(format.format(0)); System.out.println(format.format(10000)); System.out.println(format.format(100000000)); System.out.println(format.format(25.25)); System.out.println(format.format(-10.125)); Running this would display: 0.0 1

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123