package esimerkit; public class FormatMetodi { /** * Testaillaan Javan String.format metodia, sekä System.printf-metodia, jolla * voidaan tehdä muotoiltu tulostus. * * @param args ei käytössä */ public static void main(String[] args) { String s = String.format("%5.2f",3.141592); System.out.println(s); System.out.println(Math.PI); String pii = String.format("%6.2f",Math.PI); System.out.println(pii); double tieto = 10.1; double nokia = 9.36; double google = 429.17; System.out.println("Pörssikurssit 18.7.2009"); System.out.println(String.format("%-12s %10.2f €", "Tieto Oyj:",tieto)); System.out.println(String.format("%-12s %10.2f €", "Nokia Oyj:",nokia)); System.out.println(String.format("%-12s %10.2f $", "Google Inc.:",google)); System.out.printf("%-12s %10.2f $", "Google Inc.:",google); System.out.printf("%5.2f",Math.PI); System.out.println("Pörssikurssit 18.7.2009"); System.out.printf("%-12s %10.2f € \n", "Tieto Oyj:",tieto); System.out.printf("%-12s %10.2f € \n", "Nokia Oyj:",nokia); System.out.printf("%-12s %10.2f $ \n", "Google Inc.:",google); } }