473,395 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Floating Point

Hi,

A pice of code.

float A = 10;
float B = 7;
float result;

result = A / B;

when i run this program, i've got as result 1,4285714.... etcetc.
is it possible to get only two or one decimals after the floating point? And how?

Thanks in advance.
Geert
Jul 22 '05 #1
6 1276
"ge*******@pandora.Be" wrote:
Hi,

A pice of code.

float A = 10;
float B = 7;
float result;

result = A / B;

when i run this program, i've got as result 1,4285714.... etcetc.
is it possible to get only two or one decimals after the floating point? And how?

Thanks in advance.
Geert


round it
/B

Jul 22 '05 #2

"Bob Smith" <bo*******@marketweighton.com> schreef in bericht
news:3F***************@marketweighton.com...
"ge*******@pandora.Be" wrote:
Hi,

A pice of code.

float A = 10;
float B = 7;
float result;

result = A / B;

when i run this program, i've got as result 1,4285714.... etcetc.
is it possible to get only two or one decimals after the floating point? And how?
Thanks in advance.
Geert
round it
/B

And how do you do that, can you give me an example? I'm just a bit new in
C++
Thanks.
Geert

Jul 22 '05 #3
Geert Joos wrote:
"Bob Smith" <bo*******@marketweighton.com> schreef in bericht

round it
/B


And how do you do that, can you give me an example? I'm just a bit new in
C++


Here's one way:

#include <iostream>
#include <cmath>

using namespace std;

int
main()
{
float A = 10;
float B = 7;

float result = A / B;

float prec = 2.0;
float shift_factor = pow(10, prec);

cout << round(result * shift_factor) / shift_factor << '\n';

return 0;
}

Note that the above method is not particularly robust, elagent, or
efficient. It's probably not even accurate for arbitrary values of
'result'. Use at your own risk.

- Adam

--
Reverse domain name to reply.

Jul 22 '05 #4
ge*******@pandora.Be escribió:
Hi,

A pice of code.

float A = 10;
float B = 7;
float result;

result = A / B;

when i run this program, i've got as result 1,4285714.... etcetc.
is it possible to get only two or one decimals after the floating point? And how?

Thanks in advance.
Geert


If what you want is to display the number with 2 decimals although it
actually has more you can do the following:
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
float a=3.239487;
float b=23.234;
float r=b/a;

cout << setprecision(3) << r << endl;

return 0;
}
Jul 22 '05 #5
ge*******@pandora.Be wrote:
Hi,

A pice of code.

float A = 10;
float B = 7;
float result;

result = A / B;

when i run this program, i've got as result 1,4285714.... etcetc.
is it possible to get only two or one decimals after the floating point? And how?

Thanks in advance.
Geert


#include <iostream>

/* Truncate a number to (at most) two digits past the decimal point.
* NB: This implementation is naive.
*/
template< typename T >
T round2( T const& t )
{
return int( t * 100 ) / 100.0;
}

int main( )
{
float A = 10;
float B = 7;
float result;

result = A / B;

std::cout << result << '\n';
std::cout << round2( result ) << '\n';
}

Jul 22 '05 #6

"ge*******@pandora.Be" <ge*******@Pandora.Be> wrote in message
news:d7**************************@posting.google.c om...
float A = 10;
float B = 7;
float result;

result = A / B;

when i run this program, i've got as result 1,4285714.... etcetc.
is it possible to get only two or one decimals after the floating point?

And how?

float A = 10;
float B = 7;
float result;

result = ((long)((A * 100) / B)) / 100.0;

-Walter
www.digitalmars.com free C/C++/D compilers
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: JS | last post by:
We have the same floating point intensive C++ program that runs on Windows on Intel chip and on Sun Solaris on SPARC chips. The program reads the exactly the same input files on the two platforms....
5
by: Anton Noll | last post by:
We are using Visual Studio 2003.NET (C++) for the development of our software in the fields digital signal processing and numerical acoustics. One of our programs was working correctly if we are...
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
24
by: j0mbolar | last post by:
C supports single precision floating point and double precision floating point but does it support fixed floating point? i've read that fixed floating point is more accurate than single precision...
7
by: Vinoth | last post by:
I'm working in an ARM (ARM9) system which does not have Floating point co-processor or Floating point libraries. But it does support long long int (64 bits). Can you provide some link that would...
15
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
4
by: jacob navia | last post by:
Hi people I continue to work in the tutorial for lcc-win32, and started to try to explain the floating point flags. Here is the relevant part of the tutorial. Since it is a difficult part, I...
32
by: ma740988 | last post by:
template <class T> inline bool isEqual( const T& a, const T& b, const T epsilon = std::numeric_limits<T>::epsilon() ) { const T diff = a - b; return ( diff <= epsilon ) && ( diff >= -epsilon );...
39
by: rembremading | last post by:
Hi all! The following piece of code has (for me) completely unexpected behaviour. (I compile it with gcc-Version 4.0.3) Something goes wrong with the integer to float conversion. Maybe somebody...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.