Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Mathematically how do I get the cents from a dollar value (ex: $21.99$)?

As a programmer, I would simply convert to a string and grab everything after the decimal... but I would think this would be doable with pure math, or maybe I have too much faith in the black magic

EDITED

I wasn't clear.
Is it possible with pure math (no computer API.. just old school paper and pencil) to find the fractional part of any given decimal with any given precision?
Is there a formula?

Given xx.yyyy is there a formula that would return yy?

share|improve this question
    
See the floor or ceil functions and subtract the appropriate one. –  Raymond Manzoni Apr 1 '13 at 15:26
    
@RaymondManzoni I'm looking for the "math". I can get around an API –  kevcoder Apr 1 '13 at 15:44
add comment

4 Answers 4

up vote 1 down vote accepted

This can't be done in general with only (finite) arithmetic, but what you're looking for is the fractional part function.

Now, if you're just interested in dollar values, and you avoid fractions of a cent, then this is less complicated, but still not reachable through basic arithmetic only. Note that $\$21.99$ is simply an alternative notation for the dollar amount $$21+\frac{99}{100}.$$ Multiplication by $100$ yields the integer $2199,$ whose remainder when divided by $100$ is $99$, which upon multiplication by $.01$ yields the fractional part.

In general, given a dollar amount $d$ with no fractions of a cent, $$.01*\text{mod}(100*d,100)$$ will be your fractional part, where $\text{mod}(m,n)$ represents the remainder when a positive integer $m$ is divided by a positive integer $n$.

share|improve this answer
    
thanks for the detailed explanation –  kevcoder Apr 1 '13 at 18:02
add comment

Look at the number Mod 1. When you look at a number Mod n, you just divide by n, and look at the remainder. So here you divide by 1, and the remainder is just the fractional part.

share|improve this answer
add comment

The best way to do this depends on the language. For something C- or Java-based, I'd do:

 decPart = x - (int)x;

Basically, this finds the difference between the integer part and the original number.

share|improve this answer
add comment

You just subtract the integer part, getting $0.99$. If you intend to ask how to do it with some specified set of functions, you posting doesn't make that clear. For example, one could multiply it by $100$ (so that $21.83$ becomes $2183$), then find the remainder on division by $100$ (so that $2183$ becomes $83$), then divide by $100$ (so that $83$ becomes $0.83$). Or, if what you have is the floor function, you could find $x-\lfloor x\rfloor$, for example: $$ 21.83-\lfloor21.83\rfloor = 0.83. $$

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.