right truncate of float variable

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

  1. Joriveek

    Joriveek Guest

    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
    #1
    1. Advertising

  2. In article <decnko$oni$>,
    Joriveek <> wrote:
    >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?


    What do you want done if the value is (e.g.,) 3.1415926535 ?
    You would only want the 15926535 displayed?
    --
    I was very young in those days, but I was also rather dim.
    -- Christopher Priest
    Walter Roberson, Aug 22, 2005
    #2
    1. Advertising

  3. Joriveek

    Joriveek Guest

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

    Thanx

    "Walter Roberson" <-cnrc.gc.ca> wrote in message
    news:decqnt$n8v$...
    > In article <decnko$oni$>,
    > Joriveek <> wrote:
    >>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?

    >
    > What do you want done if the value is (e.g.,) 3.1415926535 ?
    > You would only want the 15926535 displayed?
    > --
    > I was very young in those days, but I was also rather dim.
    > -- Christopher Priest
    Joriveek, Aug 22, 2005
    #3
  4. In article <decr5h$pq2$>,
    Joriveek <> top-posted:

    [Note: please do not top-post: it makes conversations harder to follow]

    >"Walter Roberson" <-cnrc.gc.ca> wrote in message
    >news:decqnt$n8v$...
    >> In article <decnko$oni$>,
    >> Joriveek <> wrote:
    >>>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?


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


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


    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.
    --
    "No one has the right to destroy another person's belief by
    demanding empirical evidence." -- Ann Landers
    Walter Roberson, Aug 22, 2005
    #4
  5. Joriveek

    Joriveek Guest

    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.


    "Walter Roberson" <-cnrc.gc.ca> wrote in message
    news:decrkl$oa9$...
    > In article <decr5h$pq2$>,
    > Joriveek <> top-posted:
    >
    > [Note: please do not top-post: it makes conversations harder to follow]
    >
    >>"Walter Roberson" <-cnrc.gc.ca> wrote in message
    >>news:decqnt$n8v$...
    >>> In article <decnko$oni$>,
    >>> Joriveek <> wrote:
    >>>>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?

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

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

    >
    > 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.
    > --
    > "No one has the right to destroy another person's belief by
    > demanding empirical evidence." -- Ann Landers
    Joriveek, Aug 22, 2005
    #5
  6. In article <decrr4$pvr$>,
    Joriveek <> top-posted:

    As I requested earlier, please don't top-post -- post your comments
    *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
    likely instruct their newsreaders to ignore all of your posts.


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


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


    >I am using MSVC++ to compile the program


    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.
    --
    Feep if you love VT-52's.
    Walter Roberson, Aug 22, 2005
    #6
  7. Joriveek

    Flash Gordon Guest

    Walter Roberson wrote:
    > In article <decrr4$pvr$>,
    > Joriveek <> top-posted:


    <snip>

    >>I am using MSVC++ to compile the program

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


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

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


    So give that advice.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Aug 22, 2005
    #7
  8. "Joriveek" <> writes:
    > 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?


    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 (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Aug 22, 2005
    #8
  9. Joriveek

    Eric Sosman Guest

    [OT] Re: right truncate of float variable

    Keith Thompson wrote:
    > "Joriveek" <> writes:
    >
    >>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?

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


    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
    #9
  10. Re: [OT] Re: right truncate of float variable

    Eric Sosman wrote:

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


    In what base?

    -- glen
    glen herrmannsfeldt, Sep 27, 2005
    #10
  11. Re: [OT] Re: right truncate of float variable

    glen herrmannsfeldt <> writes:
    > Eric Sosman wrote:
    >
    >> In an episode of the comic strip "Fox Trot," the two
    >> nerdy kids dared each other to recite the digits of pi.
    >> Backwards.

    >
    > In what base?


    It's pretty easy in base pi.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Sep 27, 2005
    #11
  12. Re: [OT] Re: right truncate of float variable

    "Keith Thompson" <> wrote in message
    news:...
    > >> the two nerdy kids dared each other to recite the digits of pi.
    > >> Backwards.

    > > In what base?

    > It's pretty easy in base pi.


    :)

    Alex
    Alexei A. Frounze, Sep 27, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bd
    Replies:
    0
    Views:
    614
  2. k3n3dy
    Replies:
    15
    Views:
    986
    dan2online
    Apr 20, 2006
  3. Carsten Fuchs
    Replies:
    45
    Views:
    1,525
    James Kanze
    Oct 8, 2009
  4. Marcin Tyman
    Replies:
    7
    Views:
    201
    Josef 'Jupp' Schugt
    Jul 26, 2007
  5. Junkone

    truncate float to 2 decimals

    Junkone, Aug 13, 2008, in forum: Ruby
    Replies:
    5
    Views:
    177
    Tom Reilly
    Aug 14, 2008
Loading...

Share This Page