Place values

Pages: 12
How do I get a decimal like 0.99584585 to just 0.99?
You can't use this with in a formula can you?

BTW I use std::setprecision() all the time
You can use stringstreams.
Or if you have a mathematical rounding function ( http://www.cplusplus.com/forum/articles/3638/ )
you can do roundfunc ( yournumber*100 ) / 100
What???

1
2
3
4
/* FUNCT PROTO  */
double roundfunct (double n) {
return (n * 100) / 100;
}


That just makes it the same value

no compiling needed...
mental math...
Last edited on
Actually it tricks the compiler sorta... Just try it.
How about if you wanted a number to show 03 instead of just 3? Project I am doing is to show a time as 03/10/1983 as an example,
@beginner at programming
that wasn't what I meant. read my example more carefully.

@taegan83
Manipulators also for that, see http://www.cplusplus.com/reference/iostream/manipulators/setfill/
@Bazzy
I read the article and honestly I don't know what you mean...

I'm not trying to round...
I am trying to discard extra place values after the hundredths place....

Although you can do this by rounding, I'd prefer not to.
yea but if you multily by 100 , round the product and
then 
divide by 100 you'll get what you want. just think about it...

"I am trying to discard extra place values after the hundredths place...."..or just cast it to an int if you don't want to round
Last edited on
@Joe101
Yes... But each time you some one uses the program, The decimal will change then it won't know what to do, as far as rounding up or rounding down so if i make a function prototype of it and use either ceil() or floor() chances are it will be wrong most of the time...

So any suggestions of an alternate way...
anybody?
Last edited on
If you are trying to truncate, that should work fine. It will multiply it up to 100, taking all the values you want as an integer part, with the left over stuff you want off in decimals. It will then divide it by 100. I don't know how casting gets into it but it should work exactly as you planned it. Have you even tried it or are you discrediting it based on mathematical evidence that 100/100=1?
@tummychow
I Have no idea what you are trying to say...

For example I think you said something recently about how British education is the worst, and you learn mostly on your own. (or was it not you who said that)

Well American education is worse...

My point is, Explain that more clearly please.

Sorry for my troubles.

@Joe101

an int would make it a whole number...
Last edited on
I'm asking, have you tried it?
And no, that wasn't my line.
Think of it this way. You multiply 3.1415926 by 100. (You might have to cast it to int first, I am not sure how the compiler will respond). You end up with 314.15926. Cast it to int and you get 314. Divide by 100 and you get 3.14 - the truncation of 3.1415926, to two decimal places.
Last edited on

1
2
3
4
5
6
7
8
int round (int n) 
{
    int y = n * 100;
    int x;
    y = x;
    x = y / 100;
    return (x);
}


Still have no idea to what you are saying...



sorry tummychow that was chrisname who said that...


Last edited on
Have you actually tried it?
Yes above...

Wait what is casting
Last edited on
Casting is converting from one type to another. That's why it works. When you convert to integer, the extra places are automatically truncated off because the type has no room for them. So you multiply the DOUBLE by 100, cast the result to INT, then get a DOUBLE of the int, divided by 100.
And I meant, try it as in create it in actual code, compile and run it. Not just type it out and look at it from a new perspective.
@tummychow
okay so an int to a double vice versa.
but then the program will compile successfully except for the fact it will display a warning.
I was taught to treat compiler warnings like compiler errors.
And yes, I did create it in actual code. It is above 3 posts. But it is wrong. So I will revise it in the morning and then post if i got it wrong.
Please respond about the compiler warnings.

Sorry for the trouble of you explaining it to me about 4 times. I have been half asleep all day.
Last edited on
You can ignore most compiler type conversion warnings. Warnings are just that - warnings, not errors. In essence, they inform you that the type conversion entails a loss of data or accuracy... which is exactly what you want when truncating a floating-point value.
Pages: 12