Floating Point Variables, Working with Money, Viewer Input with Float, Constants

Now that we understand integer varialbes, let’s learn about another type of number: the floating point.

Floating Point Variables

floating point variable is a number with a decimal point value, like 5.34 or -345.98.

To declare a floating point, use float. To print a floating point, use the format specifier %f, as in this example:

#include <stdio.h>
void main( )
{
float height = 5.7f;
printf(“\nLaura is %f feet tall.\n”, height);
 

Notice the f at the end of the variable declaration: float height = 5.7f; This insures that the variable will be formatted as a floating point number.

Working with Money

Floating points work very well when you are calculating money values, particularly tax on items purchased. Let’s say that we live in a state where the sales tax is 8.25% of an item’s price. We can calculate the sales tax by multiplying it by the price, like this:

tax = price * 0.0825  

Then we can calculate the total cost of an item, including tax, like this:

total = price + tax  

Or, we could put both lines together and write:

total = price + (price * tax)  

Let’s try this in a new script, which calculates the total cost of a book, including tax:

#include <stdio.h>
void main( )
{
float price = 19.95;
float tax = 0.0825;
float total = 0.0f;
printf(“\n$%f is the price of the book.\n”, price);
total = price + (price * tax);
printf(“\nWith tax, your total is $%f.\n”, total);
 

Notice on the line float total = 0.0f; we used 0.0f instead of an empty variable. This tells the computer to make sure it’s a floating point number. Type this source code in your editor and save it as fifth.c then compile, link, and run it. You will see this output:

$19.9500000 is the price of the book. With tax, your total is $21.595875.

 

Now, you must admit that these figures looks ridiculous. Nobody thinks of money having so many decimal places. We need to limit our format to only two decimal places, for cents. Here’s how:

#include <stdio.h>
void main( )
{
float price = 19.95;
float tax = 0.0825;
float total = 0.0f;
printf(“\n$%.2f is the price of the book.\n”, price);
total = price + (price * tax);
printf(“\nWith tax, your total is $%.2f.\n”, total);
 

The %.2f tells the computer to limit the variable to 2 decimal places, so it prints in dollars and cents. Open your fifth.c source code and add this. Then save, compile, link, and run it. Now you will get this output:

$19.95 is the price of the book. With tax, your total is $21.59.

 

Isn’t that much better?

Viewer Input with Float

To handle floating point input from the viewer, we use the scanf function and the %f format specifier.

We can change how many decimal places are displayed by adjusting the %.2f to %.1f or %.3f etc. For example, ask the viewer about height:

#include <stdio.h>
void main( )
{
float height = 0.0f;
printf(“\nHow tall are you (feet.inches)? “);
scanf(“%f”, &height);
printf(“\nYou are %.1f feet tall.”, height);
 

This program will display a number with only 1 decimal place, because we have used %.1fin our printf statement.

Save this source code as sixth.c then compile, link, and run it. If you answered the question with 5.6, then your output will be:

You are 5.6 feet tall.  

Constants

If you have a variable that remains the same at all times, it is called a constant. Examples of constants are: PI (3.14159), your state’s sales tax (8.25% for example), or the American Revolutionary War (1776).

We use a special preprocessor directive, #define to set these variables. For example, if your state’s sales tax is 8.25%, you can define a constant like this:

#include <stdio.h>
#define TAX 0.0825f
void main( )
{
float price;
float total;
printf(“\nHow much would you pay for a new coffee maker? “);
scanf(“%f”, &price);
total = price + (price * TAX);
printf(“\nWith tax, your total would be $%.2f.\n”, total);
 

Notice that we used CAPITAL letters for the constant’s name (TAX). This is the standard convention for C programming and it tells us that the value of TAX will not change in the program.

Save this source code as seventh.c then compile, link, and run it. If you answered the question with 20, then your output will be:

With tax, your total would be $21.65.  

Continue on to Short & Long Variables.

Published by Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Join the Conversation

2 Comments

Leave a comment

Your email address will not be published. Required fields are marked *