i don't know how to make 1.6666667 to 1.6 only.. .

please someone help me. .

please! please! please!

Jump to Post

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 …

Jump to PostYou 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);`

Jump to Postif you want to

roundthe 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.7but what you really want to do …

Jump to PostYeah 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);`

csurfer
422
Posting Pro

**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. :)

taggz19
0
Newbie Poster

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. . .

prime1999
12
Newbie Poster

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);
```

jephthah
1,888
Posting Maven

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;
}
```

kvprajapati
commented:
Smile please. Excellent.
+5

wildgoose
420
Practically a Posting Shark

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;`

prime1999
12
Newbie Poster

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);
```

jephthah
commented:
i forgot about modf. good solution.
+12

kangarooblood
-4
Light Poster

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
by Nick Evan because:
*
n/a *

jephthah
commented:
that's the wrong answer.
-2

prime1999
12
Newbie Poster

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

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

kdeepak
-3
Newbie Poster

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

Dave Sinkula
commented:
Look at the date: if you're responding to a thread over a month old, you're probably not talking to anybody.
-2

jephthah
commented:
i think it to not possible for you to be more wrong about a basic function.
-1

kdeepak
-3
Newbie Poster

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

kdeepak
-3
Newbie Poster

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*/

jephthah
1,888
Posting Maven

please.

just.... stop.

peter_budo
2,532
Code tags enforcer
Team Colleague
Featured Poster

@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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.