# right truncate of float variable

Discussion in 'C Programming' started by Joriveek, Aug 22, 2005.

1. ### JoriveekGuest

I have a float variable like 123456789
I want to right truncate this and only display say only 8 right most values
all the time or otherwise filled by zeros if it is less than 8 digits, do
you know how can i display this?

Thanks
J.

Joriveek, Aug 22, 2005

2. ### Walter RobersonGuest

What do you want done if the value is (e.g.,) 3.1415926535 ?
You would only want the 15926535 displayed?

Walter Roberson, Aug 22, 2005

3. ### JoriveekGuest

yes...only last 10 digits....

Thanx

Joriveek, Aug 22, 2005
4. ### Walter RobersonGuest

[Note: please do not top-post: it makes conversations harder to follow]
In your original posting, you said 8, and this time you said 10,
which just happens to be the number of digits after the decimal point
that I gave. What is the "rule" -- the last 8 digits before the
deciminal point if the fractional part is 0, and the first 10 digits
after the decimal point of the fractional part is non-0 ??

You do realize, I hope, that as you get into larger values
such as 123456789012345 that the representation as a float is
inexact? For example on the system I happen to be using:

(dbx) p (float) 123456789012345
123456788103168.0

Notice that the last 7 places do not agree with the original number.

Unless there is something you haven't told us about that -greatly-
restricts the values of the float, then float is not at all a good
choice for representation of numbers when particular interior positions
are more important than magnitude.

Walter Roberson, Aug 22, 2005
5. ### JoriveekGuest

Sorry back ground is that I will be always getting a number of float type,
always wants to extract (right most) last 10 digits (sorry it's not 8
digits). how do I do this?

I am using MSVC++ to compile the program

J.

Joriveek, Aug 22, 2005
6. ### Walter RobersonGuest

*below* or interspersed with what you are commenting on. Posting your
comments at the top and then quoting the previous posting might seem
to make sense to you, but that's because the subject matter is fresh in
your mind and you know what you are trying to say. A number of us here
go through several hundred postings per day, and the only reasonable way
to keep track of what is going on is interspersed comments.

If you persist in top-posting then a number of comp.lang.c regulars will

Does the decimal point count as a digit? 12345.1234 or 12345.12345 ?

Then you should be asking in an appropriate newsgroup for your
question, such as comp.lang.c++ . This is comp.lang.c, which is for C
questions, not C++ questions.

If you were using C, then I would point you in the direction of
sprintf()... but not until warning you again that receiving your
data as a float is very going to lead to grief for your purposes.

Walter Roberson, Aug 22, 2005
7. ### Flash GordonGuest

That does *not* mean that Joriveek is using C++. I use MSVC++ regularly
to compile C code *as* C code.

Flash Gordon, Aug 22, 2005
8. ### Keith ThompsonGuest

What do you mean by the "last 10 digits"?

I think what you're asking for is (using the last 3 digits for
simplicity):

123456 --> 456
123.456 --> 456
0.123456 --> 456

But floating-point numbers are inexact. In a typical implementation,
123456 can be represented exactly, but the other two values cannot.
Assigning the value 123.456 to an object of type float might assign an
actual value of 123.45600128173828125; 0.123456 might give you
0.12345600128173828125.

And if you use double rather than float (which is usually a good
idea), you'll get different values.

There are no "last 10 digits" in any meaningful sense.

Can you redefine the problem? I presume getting the last 10 digits
isn't your ultimate goal. What are you trying to accomplish?

Keith Thompson, Aug 22, 2005
9. ### Eric SosmanGuest

In an episode of the comic strip "Fox Trot," the two
nerdy kids dared each other to recite the digits of pi.
Backwards.

Eric Sosman, Aug 22, 2005
10. ### glen herrmannsfeldtGuest

In what base?

-- glen

glen herrmannsfeldt, Sep 27, 2005
11. ### Keith ThompsonGuest

It's pretty easy in base pi.

Keith Thompson, Sep 27, 2005
12. ### Alexei A. FrounzeGuest

Alex

Alexei A. Frounze, Sep 27, 2005