Fixed Decimal in C++

Fixed Decimal

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 59444 Views - Last Post: 05 February 2009 - 01:20 PM Rate Topic: -----

#1 nano72  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-September 06

Fixed Decimal in C++

Posted 06 September 2006 - 07:56 AM

Hi Guys ,

I am looking for method that will do Fixed decimal in C++ for eg. if i have a value of type double - 0.00000008 I would expect to get that value of type double returned to six decimal places 0.000000
Another example is 12345.678 passed in I get the value 12345.678000

Any ideas why so and is there something in C++ i can used to overcome this ..

Your Help is much appreciated ,
Is This A Good Question/Topic? 0
  • +

Replies To: Fixed Decimal in C++

#2 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: Fixed Decimal in C++

Posted 06 September 2006 - 08:10 AM

You can use setprecision manipulator.
Was This Post Helpful? 0
  • +
  • -

#3 nano72  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-September 06

Re: Fixed Decimal in C++

Posted 06 September 2006 - 08:18 AM

View PostXing, on 6 Sep, 2006 - 08:10 AM, said:

You can use setprecision manipulator.

Oh nice one - thx
But that seems to take an integer where i want to pass in a double Also the This function should only be used as a stream manipulator. I want to return the value I don't want use it in a stream.
Any ideas of another method that does fixed decimal percisioning??????????????????

This post has been edited by nano72: 06 September 2006 - 09:24 AM

Was This Post Helpful? 0
  • +
  • -

#4 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: Fixed Decimal in C++

Posted 06 September 2006 - 09:36 AM

View Postnano72, on 6 Sep, 2006 - 08:48 PM, said:

Oh nice one - thx
But that seems to take an integer where i want to pass in a double Also the This function should only be used as a stream manipulator. I want to return the value I don't want use it in a stream.
Any ideas of another method that does fixed decimal percisioning??????????????????

Please explain clearly or give me a small code snippet. I am not able to understand what you mean.
Was This Post Helpful? 0
  • +
  • -

#5 BitByte  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 194
  • Joined: 09-August 06

Re: Fixed Decimal in C++

Posted 06 September 2006 - 09:37 AM

It would be better if you could actually post some code so we can see exactly what you are trying to achieve.

Are you using: #include <iomanip> ?
to use precision use cout.precision( 3 ) for 1.23 for example.

Beat me to it Xing :-)

This post has been edited by BitByte: 06 September 2006 - 09:38 AM

Was This Post Helpful? 0
  • +
  • -

#6 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Fixed Decimal in C++

Posted 06 September 2006 - 09:38 AM

check this msdn link. it explains fixed decimals using ios::fixed.
Click Here
:)
Was This Post Helpful? 0
  • +
  • -

#7 nano72  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-September 06

Re: Fixed Decimal in C++

Posted 06 September 2006 - 09:51 AM

View PostBitByte, on 6 Sep, 2006 - 09:37 AM, said:

It would be better if you could actually post some code so we can see exactly what you are trying to achieve.

Are you using: #include <iomanip> ?
to use precision use cout.precision( 3 ) for 1.23 for example.

Beat me to it Xing :-)



nope don't want to use the stream manipulator as i am not using cout << ..

Sample code
p_value = 12345.678
double cdec::dec(double p_value){
double result = p_value; // just as an eg.
return result;

}

Now this result can be passed into some other method but if I use an interpreted language (tcl) to see what the result is by calling the cdec method - I will get 12345.678000
expected result 12345.678

Can you follow ?
So i thought there might be some algorithm/method out there that does this manipulation correctly .. sure there is ..
any ideas ?
thanks
nano72
Was This Post Helpful? 0
  • +
  • -

#8 Mrafcho001  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 41
  • View blog
  • Posts: 769
  • Joined: 01-November 05

Re: Fixed Decimal in C++

Posted 06 September 2006 - 07:01 PM

cout << setw(number)
Was This Post Helpful? 0
  • +
  • -

#9 nano72  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-September 06

Re: Fixed Decimal in C++

Posted 07 September 2006 - 01:26 AM

View PostMrafcho001, on 6 Sep, 2006 - 07:01 PM, said:

cout << setw(number)


Thanks , but I don't want to use string manipulators as I will not be using cout<<<

Is there an algorithm[b] out there that can do fixed decimal percisioning not using the cout << methods


??????????????????????????

Cheers

This post has been edited by nano72: 07 September 2006 - 01:44 AM

Was This Post Helpful? 0
  • +
  • -

#10 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Fixed Decimal in C++

Posted 07 September 2006 - 05:23 AM

Are you looking for a rounding algorithm?
Was This Post Helpful? 0
  • +
  • -

#11 nano72  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-September 06

Re: Fixed Decimal in C++

Posted 07 September 2006 - 05:29 AM

View PostAmadeus, on 7 Sep, 2006 - 05:23 AM, said:

Are you looking for a rounding algorithm?


Yes that would be great if you know one that can round to any size but for this exampl i use .01 decimal places for eg.

.085 combination rounded to get .09
.086 - rounded expect to get .09
.081 - rounded expect to get .08
.08 - rounded expect to get .08

any ideas of algorithm that can do the above
Was This Post Helpful? 0
  • +
  • -

#12 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Fixed Decimal in C++

Posted 07 September 2006 - 06:03 AM

You can use printf() if you are willing to use it.
double a=3.6;
printf("%f - Without Modifier\n",a);
printf("%.2f - With Modifier",a);


Was This Post Helpful? 0
  • +
  • -

#13 nano72  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-September 06

Re: Fixed Decimal in C++

Posted 07 September 2006 - 06:24 AM

nope don't want to use any stdio o/p printf or cout ...

Waht I really am looking for is an algorithm that will do the above
have made a shout of something here myself
double x = 0.085;
double m = pow( 10.0, .01 );
x = (int)(x * m + 0.5) / m;
but i am not getting my desire results as outlined above
Any ideas out there ????
Was This Post Helpful? 0
  • +
  • -

#14 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Fixed Decimal in C++

Posted 07 September 2006 - 07:01 AM

Are the values always going to be fairly small? If so, one option may be to multiply your number by ten to the power of x (x being the number of decimal places you want), convert it to a long, convert back to a double, and divide by ten to the power of x. Note that large numbers could produce an overflow quite quickly, however.

you might also take a look at using the sprintf() function for conversion and decimla placing, then reconvert to a float/double.

Both approaches are pretty cumbersome, however.
Was This Post Helpful? 0
  • +
  • -

#15 nano72  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 06-September 06

Re: Fixed Decimal in C++

Posted 07 September 2006 - 07:37 AM

View PostAmadeus, on 7 Sep, 2006 - 07:01 AM, said:

Are the values always going to be fairly small? If so, one option may be to multiply your number by ten to the power of x (x being the number of decimal places you want), convert it to a long, convert back to a double, and divide by ten to the power of x. Note that large numbers could produce an overflow quite quickly, however.

you might also take a look at using the sprintf() function for conversion and decimla placing, then reconvert to a float/double.

Both approaches are pretty cumbersome, however.


Thanks
In this case yes ...

Can you show me a code example i was a bit lost when you say convert from a double to a long and back .......

This post has been edited by nano72: 07 September 2006 - 07:59 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2