Not Yet Answered # how to round off using float

csurfer 422 Discussion Starter taggz19 prime1999 12 jephthah 1,888 wildgoose 420 prime1999 12 kangarooblood -4 prime1999 12 kdeepak -3 kdeepak -3 kdeepak -3 peter_budo 2,516 Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...

0

**You can do these :**

**1>**Well if its just a matter of printing then `"%.nf"`

where n is the number of digits after the `"."`

you want to print in a floating point number.

**2>**If its really about conversion then you can know the IEEE floating point format for your machine and compiler architecture and then mask off the excessive bits by `"logical anding"`

which ofcourse gets you into some troubles as you don't exactly know the number stored there and its stored in

<1bit-signbit><8bit-signed exponent><23bit-mantissa> for single precision 32 bit float format.

**3>**So may be you can do this :

* char num[10];

* sprintf(num,"%1f",<your actual float variable>);

* Now traverse through the array num[] and get the digits before "." the "." and digits after "." and then convert it into a number.

If there are any other ways even I would like to know. :)

0

thanks for the info, but when i enter "%.1f" it results into 1.7 and not 1.6. .

what should i do??

please help me. . .

0

You could do this:

```
// #include<cstring>
float t=31.4592;
char num[100];
sprintf(num,"%f",t);
p=strchr(num,'.');
if( p!=NULL ) num[p-num+2]=0;
printf("%s\n",num);
```

or this:

```
// #include<cmath>
float t=31.4592;
printf("%.1f\n",floor(t*10)/10);
```

2

if you want to **round** the number, then you use "setprecision(<n>)" or the format specifier "%.<n>f" for "printf()" where <n> is the number of digits you want to round to. E.g., the value 1.66667 rounded to <n>=1 decimal place will be 1.7

but what you really want to do here is **truncate**. this means just "chopping off" (so to speak) all digits past the desired place will just be thrown out, and the least significant remaining digit will not be rounded.

to truncate a value, then, you need to use the "floor" function (for positive values, see caveat* below). but since this returns only a whole number, you will need to first "shift" the value to be truncated a number of decimal places that you want to truncate, floor the value, then shift them back.

(note my use of the word "shift" here is not a bitwise shift, because we're talking about decimal places, not binary places -- you will have to multiply by powers of 10)

```
value = 1.66667;
truncValue = value * pow(10,1); // shifts dec. pt. 1 place right
truncValue = floor(truncValue);
truncValue /= pow(10,1); // shifts dec. pt. 1 place left
```

you can replace the '1' with a variable to allow variable truncation.

*caveat: this will not work for negative numbers. example value = -1.666667 **rounded** to 1 decimal point would be -1.7, and **truncated** to 1 decimal point should be -1.6.

However, if you used "floor" on this negative value, the result would be -1.7. Therefore, negative values need to use the converse function "ceil()" which would give the expected value -1.6.

now you can make a function like so:

```
double truncate (double value, int places)
{
if (value>0)
{
// do the routine with "floor"
}
else
{
// do the routine with "ceil"
}
return truncValue;
}
```

0

The float to ASCII, clip, then ASCII to float is one method.

You're only truncating by one decimal place! Not I said clip not round.

So one method is

`f = floor(N * 10.0f) * 0.1f;`

1

Yeah I didn't realise your "caveat" that negative values would be truncated wrongly... but if you want an easier way other than if loops:

```
// #include<cmath>
float t=31.4592,x;
modf(t*10,&x);
printf("%.1f\n",x/10);
```

-1

use the iomanip header

```
#include <iomanip.h>
#include <iostream>
using namespace std;
int main()
{
cout << "enter two numbers that you want to divide >> ";
double a;
double b;
cin >> a;
cin >> b;
cout.precision(2); // only display 2 numbers
cout << a << " / " << b << "= " << a/b;
cin.get();
cin.get();
return 0;
}
```

this will give u two number in precision.

*Edited 6 Years Ago by Nick Evan*: n/a

0

Look at post #3... the original poster seems to want to truncate the float to 1 dp.

And please **close** your code tag properly!

-4

I think it to not possible to round off 1.666666 to 1.6

the only thing u can do is u can restrict the number of digits u display after floating by

i=1.66666

printf("%0.1f",i);

output

1.6

-3

I think it to not possible to truncate 1.666666 to 1.6

the only thing u can do is u can restrict the number of digits u display after floating by

i=1.66666

printf("%0.1f",i);

output

1.6

-2

first multiply the number by 10

then use ceil() function to round it to integer value

then divide it by 10

u will get wat u want

eg:

int i;

float j=1.6667;

j=j*10; /* 16.677 */

i=ceil(j); /*17*/ i=floor(j);/*16*/

j=i/10; /*1.7*/ j=i/10; /*1.6*/

0

@kdeepak in the future please make sure you do not reply old dead threads, and if you do make sure you have bullet proof reply not guesses as above.

Thread closed.

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Hello All ...

Iam Getting An Error With try to excecute the stored procedure .

I have Have Sql database , the stored procedure like so :

```
USE [MPRS]
GO
/****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE ...
```

Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"

Here is the sample output that I ...