
Formatting Decimals in Java
Formatting Decimals in Java
null
Content
0 -> There are several different ways that we
1.979 -> can format output in Java. We can use the
4.319 -> math function so that we can round them
6.99 -> up or down. We can use the printf( ) or the
9.96 -> print format method or we can use the
11.82 -> decimal format class. This video is going to show you how to
14.79 -> use the printf( ), the print format method,
16.65 -> and the decimal format class. You can
19.619 -> control the number of decimal places
20.76 -> displayed when you use a floating point
23.07 -> value and a printf( ) statement by adding
25.26 -> the optional precision factor to a
27.359 -> format specifier. We can convert our
29.609 -> print line statement into a print format
31.59 -> quite easily. As you see right now we'll
33.75 -> just take the string each child will
35.19 -> receive $ plus the results
37.86 -> inside the variable.
39.49 -> We will change this to be a printf( )
41.2 -> statement now or copy and paste it to
43.12 -> start and change from print line to
45.91 -> printf( ).
48.1 -> Now we have to fix the printf( ) statement
51.219 -> that will place in the variable that we want
54.46 -> with the format specifier. The % tells
58.03 -> java that where this starts is where
60.399 -> we're going to put in our variable that
62.649 -> we're going to need to be formatted
65.08 -> appropriately. After the %, we tell it that
67.81 -> we want it to have a decimal place with
70.509 -> two decimal places showing, and it'll be
73.479 -> treated as a float. Now we get rid of our
76.869 -> plus sign and we turn that into a comma.
79.03 -> It will come along here it will read each
80.979 -> child will receive and it comes to this percent sign and
84.1 -> that tells it whatever character or
86.319 -> whatever variable is in the first spot
87.729 -> that is what we're going to put in here
89.35 -> and we're going to format it to be a floating-point decimal with two
93.67 -> decimal places. I'll run this, and you can
97.509 -> see how it's converted it to be floating
99.759 -> point number with two decimal places. If I
101.649 -> want a period at the end all I have to do is add
103.539 -> that period here and it will display it
105.67 -> as well. So that's one way you can do it
107.409 -> by using a printf( ) statement. Another way
110.08 -> you can do it is by using the decimal
111.94 -> format class. The decimal format
113.68 -> class is a lot more useful for other
115.509 -> means of printing decimals. The decimal
118.06 -> format class provides an easy way to
119.56 -> convert numbers into strings and allows
121.329 -> you to control the display of the leading
123.159 -> and trailing zeros, any prefixes, suffixes,
125.5 -> groupings, separators, decimal points, all
129.67 -> that kind of fun and exciting stuff can
131.739 -> be formatted in the properties of a
133.66 -> decimal format. Now the decimal format is
136.03 -> a class which we have not used yet, but
138.31 -> it uses a string pattern to change the
140.89 -> variable to match what it needs to
143.29 -> look. We use a constructor, which you
145.93 -> learn about later, to tell the decimal
148.15 -> format what we want it to look like. The
151.72 -> decimal format class is a class that
154.54 -> we have to import into Java which again
156.4 -> is something that we haven't done up
157.57 -> into this point but importing packages
160.39 -> and classes give us more flexibility and
163.54 -> customization of our java programs. To
165.88 -> create a decimal format, we're going to
167.29 -> begin by creating an instance of the
170.049 -> decimal format class and we're gonna tell that
171.79 -> what we wanted to look like.
176.68 -> I have to import my decimal format
178.959 -> package, so i'm going to click the error
180.7 -> message and click import format decimal.
182.68 -> It's going to add an import statement up here
187.629 -> to the very top of my java file. I've set
191.14 -> my decimal format pattern so that it
193.51 -> will display with whatever value but it
195.819 -> will have a spot for
198.37 -> two zeros at the end. So this means that
200.709 -> even if each child was just to receive
202.54 -> $13, it was still print 13.00. There's
206.5 -> also an optional printing of the decimal
209.26 -> places. This pound sign means that if
212.019 -> there are numbers there it will print and if
214.06 -> there are no numbers there it won't
215.5 -> print. So, we want to change it back so
217.959 -> that no matter what it's going to print
219.159 -> the two decimal places. Now to use this
222.159 -> decimal format, I have to tell it to
224.439 -> apply that decimal formatting to a value
227.709 -> or to a number. So, I want to take my item
230.829 -> back up here, my print line, want to copy
232.87 -> it down here and I'm going to tell it
236.919 -> use the decimal format that i just
238.989 -> created on this particular variable.
246.74 -> I'm actually calling the format method
249.2 -> on this variable and it's going to make
251.78 -> it so that it will display two
257.6 -> decimal places. I need to do some
259.76 -> formatting. You can see my printf( ) did not
262.04 -> create a new line whenever it's done so
263.78 -> i'm going to cheat and make it have a
266.15 -> new line at the end. There we go.
270.92 -> Now I get my decimal format
274.76 -> to format my variable and I'll need to add
278.24 -> a decimal or a period at the end too, just
281.15 -> like before. So these are two ways that you can
285.65 -> format decimal places of doubles or
289.01 -> float values. The first one's using the
290.57 -> printf( ), and again that's just limited to
292.43 -> the console, but the second one is using
294.35 -> the decimal format class which will
296.99 -> allow me to apply a decimal formatting
299.33 -> to any kind of variable.
Source: https://www.youtube.com/watch?v=G7IbADolOl4