TestFormat - PowerPoint PPT Presentation

About This Presentation
Title:

TestFormat

Description:

{ public static void main(String [] args){ Terminal output = new ... { deci = (DecimalFormat)deci.getInstance(Locale.FRANCE); output.println(deci.format(number[x] ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 9
Provided by: ricardo65
Learn more at: https://www.cs.umb.edu
Category:

less

Transcript and Presenter's Notes

Title: TestFormat


1
TestFormat
  • import java.text.
  • import java.util.
  • public class TestFormat
  • public static void main(String args)
  • Terminal output new Terminal()
  • double number 1120.0, 111225.500,
    50000.67
  • DecimalFormat deci new
    DecimalFormat(",,.")
  • for(int x 0 x lt number.length x)
  • output.println(deci.format(numberx))

2
output
  • 1,120
  • 111,225.5
  • 50,000.67

3
double number 1120, 111225.500, 50000.67,
1000000076.989
  • 1,120
  • 111,225.5
  • 50,000.67
  • 1,000,000,076.99

4
,,.
  • The number can have at most 2 digits after the
    decimal point
  • The NumberFormat doesnt just drop the extra
    digits but rounds up
  • ,. would have worked the same
  • . would not have the ,

5
For those in France
  • import java.text.
  • import java.util.
  • public class TestFormat
  • public static void main(String args)
  • Terminal output new Terminal()
  • double number 1120, 111225.500,
    50000.67, 1000000076.989
  • DecimalFormat deci new
    DecimalFormat(",.")
  • for(int x 0 x lt number.length x)
  • deci (DecimalFormat)deci.getInstance
    (Locale.FRANCE)
  • output.println(deci.format(numberx))

6
output
  • 1?120
  • 111?225,5
  • 50?000,67
  • 1?000?000?076,989
  • Notice it didnt round up like before

7
AlignIntegers from the web
  • public class AlignIntegers
  • static String align(NumberFormat fmt, int n,
    int sp)
  • StringBuffer buf new StringBuffer()
  • FieldPosition fpos new
    FieldPosition(NumberFormat.INTEGER_FIELD)
  • fmt.format(n, buf, fpos)
  • for (int i 0 i lt sp -
    fpos.getEndIndex() i)
  • buf.insert(0, ' ')
  • return buf.toString()
  • public static void main(String args)
  • NumberFormat fmt NumberFormat.getInstanc
    e()
  • System.out.println(align(fmt, 10, 9))
  • System.out.println(align(fmt, 100, 9))
  • System.out.println(align(fmt, 1000, 9))

8
output
  • 10
  • 100
  • 1,000
  • 10,000
Write a Comment
User Comments (0)
About PowerShow.com