# floating point value

This topic is 1659 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am able to input a number like 3.15 into a variable. I want to extract the 3 from the value and use it later. Let me know if you need more info.

##### Share on other sites

cast it to an int.

##### Share on other sites

cool I will try that

##### Share on other sites

In addition to the obvious answer of casting it to an int, you could try passing it to std::floor() in <cmath>, if it's just going to be converted back to a floating point number anyway. Beware, though, that casting to an int always truncates toward zero, while calling std::floor() with a negative parameter will round toward negative infinity. If your floating point number is outside of the range representable by an integer, you must use std::floor().

##### Share on other sites

is there anyway to extract the .51 from a variable.

##### Share on other sites

Subtract the integer part from the value.

##### Share on other sites

well thanks for all the help

##### Share on other sites

Or just try:

3.15 mod 1 = 0.15

3.15 - 0.15 = 3

##### Share on other sites

Since this is C++, there's a handy little C function for this... goes by the name of modf.

##### Share on other sites

well this code works except that pence outputs 0.2 and I want it to output 2



float pounds=0, shillings=0, pence = 0;

float decimal_pounds;
cout <<

"Enter decimal pounds: ";
cin >> decimal_pounds;
cout << endl;
pounds=(

int)decimal_pounds;
decimal_pounds= decimal_pounds - pounds;
shillings = (

int) (decimal_pounds * 20.0);
pence = (

float)(decimal_pounds * 20.0)- shillings ;
cout <<

"Equivalent in old notation = " << '\x9c' << pounds << "." << shillings << "." << pence << endl;
system (

"pause");

return 0;


##### Share on other sites
First off, when dealing with money, avoid floats entirely. Floating point inaccuracies can trivially make generating the correct amount of change WRONG very quickly. This is why most languages that deal with banking have a "decimal" datatype, that is base 10.

Secondly, use modf, as I mentioned above. Makes it pretty trivial:
#include <iostream>
#include <string>

int main() {
float decimal_pounds;
std::cout << "Enter decimal pounds: ";
std::cin >> decimal_pounds;

float pounds, shillings, pennies;
float remainder;
remainder = modf(decimal_pounds, &pounds);

remainder *= 20.0; //convert to shillings
remainder = modf(remainder, &shillings);

remainder *= 12.0; //convert to pennies
remainder = modf(remainder, &pennies);

std::cout<<pounds<<" - "<<shillings<<" - "<<pennies<<" - "<<remainder<<std::endl;
return 0;
}
Take note that if you use a value like 12.23 you get a result out with a remainder whose value is NOT the expected 0.2, but will be a value close to 0.2! This is due to floating point errors.

As an additional note, if you accumulate those lost decimal points into an account... you end up having to burn down your place of work and take up construction. Edited by Washu

##### Share on other sites

well I tried  your code and it works fine, but I am still confused on how to calculate pence from my code, I really want to learn how to code.

##### Share on other sites

well I tried  your code and it works fine, but I am still confused on how to calculate pence from my code, I really want to learn how to code.

Well, i suggest stepping through your code with a debugger. The bug is quite obvious, and learning to use a debugger will help you to understand more.

cool thanks

##### Share on other sites

one more little question, how do I put a number like 10.2.8 into a variable.

##### Share on other sites
That's not a number.

##### Share on other sites

Given that you're using a weird currency and want to get things as integers instead of decimals it'd probably be simplest to just make a class that represents the currency and has methods to get it in different values.

But you might have to read up on classes to do that one.

##### Share on other sites

You would write a class that represents pounds, shillings and pence and would provide constructors to initialise it, and provide public methods (and/or free functions taking an object of your class as an argument) to do things with it that would be useful, like adding, subtracting, getting a string representation, etc.

EDIT: An internal representation as an integer number of pennies is one way of doing it, although not if you want farthings or halfpennies to be represented too.

##### Share on other sites

I have not got to classes yet in my book, there must a simple way of storing pounds shillings and pence.

##### Share on other sites

There is, just store the number of pence, and output it in the correct format via a function call.

Then you would wrap that in a class. It's similar to how computers could store the date, they could store the number of milliseconds since some agreed start point, but provide functions to output it in a nice format (and functions to convert various formats for the date into the internal representation). EDIT: And provide other functions to do common tasks with dates.

##### Share on other sites

There is, just store the number of pence, and output it in the correct format via a function call.
can  you give me a hint on how do that

##### Share on other sites

There is, just store the number of pence, and output it in the correct format via a function call.

can  you give me a hint on how do that

Division is involved.

##### Share on other sites

Even more of a hint:

string ConvertPenceToString( int pence )

{

string result;

/* compute each of the three values, store in the string instead of cout */

return result;

}

##### Share on other sites
Yet another hint:

Multiplication is involved.

##### Share on other sites
Lets start off with a few utility functions...

// Takes in a string of the format ##.##.##.
// returns an integer value representing the total number of pence equivalent to the string.
// Example: 12.5.4 returns 2944
int to_pence(std::string const& value);

// Takes in three integers representing pounds, shillings and pence
// returns an integer value representing the total number of pence equivalent.
// Example: 12.5.4 returns 2944
int to_pence(int pounds, int shillings, int pence);

// returns the whole number of pounds from a pence pile.
// Example: With 12.5.4 pounds it returns 12
int pounds_part(int pence);

// returns the whole number of shillings AFTER subtracting out all the pounds.
// Example: With 12.5.4 pounds it returns 5
int shillings_part(int pence);

// returns the whole number of pence AFTER subtracting out the shillings and pounds.
// Example: With 12.5.4 pounds it returns 4
int pence_part(int pence);

// Returns the total number of pounds equivelent to the pence pile.
// Example: 12.5.4 returns 12.266...
double to_pounds(int pence);

// Returns the total number of shillings equivelent to the pence pile.
// Example: 12.5.4 returns 254.333...
double to_shillings(int pence);

// Returns the string value of a pence pile.
// Example: 2944 pence returns "12.5.4"
std::string pence_to_string(int pence);

Edited by Washu