Mr G s Java Jive. #11: Formatting Numbers

Similar documents
The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Paul's Online Math Notes Calculus III (Notes) / Applications of Partial Derivatives / Lagrange Multipliers Problems][Assignment Problems]

5 R1 The one green in the same place so either of these could be green.

4.7 Approximate Integration

Table of Laplace Transforms

Chapter01.fm Page 1 Monday, August 23, :52 PM. Part I of Change. The Mechanics. of Change

9 R1 Get another piece of paper. We re going to have fun keeping track of (inaudible). Um How much time do you have? Are you getting tired?

The first thing we ll need is some numbers. I m going to use the set of times and drug concentration levels in a patient s bloodstream given below.

Lecture 1: Overview

1.2 Adding Integers. Contents: Numbers on the Number Lines Adding Signed Numbers on the Number Line

Introduction to L A TEX for MCS-236

Animations involving numbers

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

UV Mapping to avoid texture flaws and enable proper shading

Here are a couple of warnings to my students who may be here to get a copy of what happened on a day that you missed.

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

Part II Composition of Functions

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

These are notes for the third lecture; if statements and loops.

CS125 : Introduction to Computer Science. Lecture Notes #11 Procedural Composition and Abstraction. c 2005, 2004 Jason Zych

Intro. Scheme Basics. scm> 5 5. scm>

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

(I m not printing out these notes! Take your own.)

Fractions and their Equivalent Forms

Math 25 and Maple 3 + 4;

Lab 4. Out: Friday, February 25th, 2005

AP Computer Science Unit 1. Writing Programs Using BlueJ

Variables and Data Representation

2016 All Rights Reserved

1. You re boring your audience

Java/RealJ Troubleshooting Guide

Boolean Expressions. Is Equal and Is Not Equal

How To Get Your Word Document. Ready For Your Editor

Using TLC to Check Inductive Invariance

Lecture 4: examples of topological spaces, coarser and finer topologies, bases and closed sets

We re going to start with two.csv files that need to be imported to SQL Lite housing2000.csv and housing2013.csv

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

textures not patterns

IMPORTANT WORDS AND WHAT THEY MEAN

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

Part 1. Summary of For Loops and While Loops

1.7 Limit of a Function

Paul's Online Math Notes. Online Notes / Algebra (Notes) / Systems of Equations / Augmented Matricies

How to approach a computational problem

T H E I N T E R A C T I V E S H E L L

Introduction to Java Unit 1. Using BlueJ to Write Programs

EEN118 LAB FOUR. h = v t ½ g t 2

Here are some of the more basic curves that we ll need to know how to do as well as limits on the parameter if they are required.

AP Computer Science Unit 1. Writing Programs Using BlueJ

Text Input and Conditionals

Troubleshooting An Embedded Sametime Install by Julian Robichaux, panagenda originally published on socialbizug.org, November 2013

6 Stephanie Well. It s six, because there s six towers.

CALCULUS II. Parametric Equations and Polar Coordinates. Paul Dawkins

If Statements, For Loops, Functions

Cryptography III Want to make a billion dollars? Just factor this one number!

Close Your File Template

COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22

Introduction to Programming

FIGURING OUT WHAT MATTERS, WHAT DOESN T, AND WHY YOU SHOULD CARE

Bryan Kreuzberger, Creator of The Breakthrough System Presents. Breakthrough BLUEPRINT

Hey there, I m (name) and today I m gonna talk to you about rate of change and slope.

Boolean Expressions. Is Equal and Is Not Equal

4. Java Project Design, Input Methods

Clickbank Domination Presents. A case study by Devin Zander. A look into how absolutely easy internet marketing is. Money Mindset Page 1

What You Need to Know When Buying a New Computer JackaboutComputers.com

Creating a new form with check boxes, drop-down list boxes, and text box fill-ins. Customizing each of the three form fields.

An introduction to plotting data

Outlook is easier to use than you might think; it also does a lot more than. Fundamental Features: How Did You Ever Do without Outlook?

Case Study: Best Strategy To Rank Your Content On Google

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Printing Envelopes in Microsoft Word

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Introduction to Counting, Some Basic Principles

The Problem, the Solution and the End FinalPart

INTRODUCTION. 2

Fractions and their Equivalent Forms

Making a PowerPoint Accessible

Chapter 7. Polygons, Circles, Stars and Stuff

Life, the Universe, and CSS Tests XML Prague 2018

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise

Devel::Cover - An Introduction

EEN118 LAB FOUR. h = v t ½ g t 2

Math Fundamentals for Statistics (Math 52) Unit 3: Addition and Subtraction. Scott Fallstrom and Brent Pickett The How and Whys Guys.

Welcome Back! Without further delay, let s get started! First Things First. If you haven t done it already, download Turbo Lister from ebay.

Graphing by. Points. The. Plotting Points. Line by the Plotting Points Method. So let s try this (-2, -4) (0, 2) (2, 8) many points do I.

Section 0.3 The Order of Operations

Spam. Time: five years from now Place: England

Burning CDs in Windows XP

beyond the install 10 Things you should do after you install WordPress by Terri Orlowski beyond the office

Transcriber(s): Aboelnaga, Eman Verifier(s): Yedman, Madeline Date Transcribed: Fall 2010 Page: 1 of 9

Solution Guide for Chapter 21

CIS220 In Class/Lab 1: Due Sunday night at midnight. Submit all files through Canvas (25 pts)

Binary, Hexadecimal and Octal number system

13 TIPS TO IMPROVE YOUR WEB SITE

Proofwriting Checklist

How to Rescue a Deleted File Using the Free Undelete 360 Program

12 Macros. Localmake. is an abbreviation for the two instructions. localmake "fred 87. local "fred make "fred 87

Beyond the Annual Report

Divisibility Rules and Their Explanations

Creating Custom Financial Statements Using

Transcription:

Mr G s Java Jive #11: Formatting Numbers Now that we ve started using double values, we re bound to run into the question of just how many decimal places we want to show. This where we get to deal with that issue. Math Isn t Pretty Back in the 70s (yes, way before any of you were born), comedian Steve Martin used to say that comedy isn t pretty. Well, neither is math. In fact, sometimes math is just plain old ugly uglier than it really needs to be. Do you need proof? Check out the following four runs of Geo1: In the first run, Jael entered two whole number values (remember that integers are a subset of reals), and her results came out looking quite nice. There was an unnecessary decimal place and a 0 with her whole number results, but what she got looked pretty good. In the second run, Derek entered a real number and a whole number, and his results came out looking fairly decent too. In fact, they came out to two whole numbers (do you know why the math just happened to work out that way?). No problem here. Mr G s Java Jive: Formatting Numbers Page 11-1

In the third run, Pam entered two real numbers, and her results still didn t look too bad. But take a look at Adrienne s. Her area results came out with five decimal places. That s not really bad as things go, but maybe she didn t really want that many. Maybe all anyone really needs here is three places (especially when you get into results with repeating decimals). To set decimal places the way we want them, we need to learn how to use the DecimalFormat class. Much More Than You Need To Know If you want to find out all there is to know about the DecimalFormat class, you could check out the documentation page at <http://java.sun.com/j2se/1.5.0/docs/api/java/text/decimalformat.html>. But that s some pretty dense reading for a beginner in fact, it s pretty dense even for me, so I ll give you the condensed, Reader s Digest version. Importing the Tools The DecimalFormat class contains tools that let you format a number based on a certain String pattern. But you can t just use this straight out of the box. If you tried creating a decimal format of your own right now, it wouldn t compile. That s because you need to import the tools you need to do it. So make the change shown below to Geo1: //Geo1 //a program to figure out the area and perimeter of a rectangle //8.10.06 Clark Kent import java.text.*; //needed to do number formats public class Geo1 {//start class The line import java.text.* tells the compiler to import everything from the class java.text. The * character is a wildcard that means everything. It s a lot easier that way. From now on, you ll put this line at the beginning of all of your programs. Creating A Decimal Format Now that we ve told the compile to import the necessary external code, it s time to create a simple number format that sets everything to three fixed decimal places. We ll call it d3f (for decimals, three, and fixed). Check out the changes below: //variables String name; double height, width, area, perimeter; //formats DecimalFormat d3f = new DecimalFormat("0.000"); //get input The first thing you should notice here is that we ve added a new section, just below the one for variables. It s called formats. Most Java texts put the formats in with the variables, but I figured it would be a good idea to keep them separate, for the sake of clarity. The second thing you should notice is how we create a new number format. Did you catch that we re creating a new DecimalFormat and that DecimalFormat is capitalized? Did you remember that that means it s an object? So we ve said that we want to create a new DecimalFormat object called d3f, and that it ll be a new DecimalFormat object based on the string 0.000. What does this mean? It means that our numbers will always show three decimal places. It also means that there will always be at least one whole number place even if it s 0. Mr G s Java Jive: Formatting Numbers Page 11-2

Using Our Number Format Just creating the number format doesn t solve the problem for us. We have to use it, and the easiest way to use it right now is to create two new String objects that the formatted numbers will go into. Check out the changes shown below: //variables String name, astring, pstring; double height, width, area, perimeter; Now that we have these Strings, we need to add two lines to the calculations section: //do calculations area=height*width; perimeter=2*(height+width); astring=d3f.format(area); pstring=d3f.format(perimeter); Those two new lines mean to apply the d3f format to the value and put them into the String. Now all we have to do is change our final output code, and we can see some impressive results. Try this: //give output System.out.print("\nYour rectangle is has an area of "+areaastring+", "); System.out.println("and a perimeter of "+perimieterpstring+"."); System.out.println("\nSee you later, "+name+"!"); Compile it and run it with Adrienne s previous values of 4.125 and 3.25, and see what a difference it makes! That s a whole lot better, isn t it? Now let s take a closer look at our formatting options. Three Simple Rules For Formatting Numbers As I said before, I m not going to make you read the entire document on the DecimalFormat class. You don t need that level of detail. All you need to know are some simple rules about creating a format of your own. Here they are. 1. All decimal formats are defined as String patterns. This means that you can define the format directly or indirectly. More on that later. 2. A 0 in the format pattern means a required place that, if necessary, will show up as 0. 3. A # in the format pattern means an optional place, that won t show up at all if it s not a significant digit. Mr G s Java Jive: Formatting Numbers Page 11-3

A New Number Format: d3o Now that you ve created and used a number format that sets up a fixed number of decimal places, it s time to try one that has optional ones. We ll also take this opportunity to learn what I meant when I said that the patterns could be defined either directly or indirectly. But first let s run the program again using the two whole number values that Jael started off with. The output is shown below: Even though they re both whole numbers, the area and perimeter values are shown with three decimal places. Seems like a bit of a waste to me. We ll fix this by creating a new format: d3o (for decimals, three places, and optional. Check out the following code changes: //variables String name, astring, pstring; String fmt = "#.###"; double height, width, area, perimeter; //formats DecimalFormat d3f = new DecimalFormat("0.000"); DecimalFormat d3o = new DecimalFormat(fmt); The first change we made was to create a String object called fmt. This object contains the pattern we want to use to define our new format. The second change we made was to define the new format, d3o, as being a new DecimalFormat based on fmt. This is different from the way we defined d3f because here we re defining the format indirectly, by calling a previously created string with the pattern in it. The format d3f was defined directly, in one step. Pay careful attention to the fact that when you define a format directly the argument in the parentheses has to be in quotation marks since you re trying to pass a string into it. On the other hand, when you define a format indirectly, you don t put quotation marks around the argument. That s because you re passing a String object into it that s already been defined once using the quotation marks. Putting quotation marks around the string name will give you some rather interesting results. Advantages of Defining Formats Indirectly Why would anyone want to define their formats indirectly, since it involves an extra step? There actually are some advantages, although I ll admit that most of the time it actually is easier just to define them directly. The one advantage that defining your formats indirectly gives you is that it allows you to redefine formats on the fly. If you re really dying to find out what I m talking about right now, take a look in the gatling.java class at the second ralign method. That s all I ll say about that for now. Let s go to the next page to actually use our new format. Mr G s Java Jive: Formatting Numbers Page 11-4

Using Our New Format This is going to be really simple. just make the following two changes in your code: //do calculations area=height*width; perimeter=2*(height+width); astring=d3fo.format(area); pstring=d3fo.format(perimeter); In case that was unclear, I simply changed the two instances of d3f to d3o. Now when you compile and run with the values 3 and 4, you should get output like this: Since the decimal places weren t needed, they weren t shown. But what about Adrienne s values? Here s what happens when we run them through with the new format: Here, the area maxes out at three decimal places, but the perimeter maxes out at two, and there are different reasons for each. Area maxes out at three places because our format was set up to show at most three decimal places. Perimeter maxes out at two places because the third place is a 0, which in this case is not a significant digit, so it gets dropped. Fixed vs Optional Places So now that you know how to set up formats for both, how do you know when to use fixed or optional decimal places? Unfortunately (or maybe fortunately), as with many things in life, there is no hard and fast rule. A lot just depends on how you want it to look and how many decimal places you want a number to go to. Obviously, if you re working with money, you ll always want two fixed decimal places (unless you re working with large amounts of money, and then, who cares about the pennies). However, aside from that obvious consideration, the number of decimal places, and whether or not they re optional, is pretty much up to you within reason (only the most geeky users want to see more than three or four decimal places). It s Not Perfect As good as it is, to my mind, the NumberFormat class and its methods aren t perfect. It would be perfect if it let you set up the total column width for the number (C++ lets you do that). But since it doesn t, I took matters into my own hands, and wrote a few methods that will do that for you. We ll look at them later much later. Mr G s Java Jive: Formatting Numbers Page 11-5

This page intentionally left almost blank Mr G s Java Jive: Formatting Numbers Page 11-6