Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Removing digits after decimal point

    Hi guys,

    I have var1 where values can be 10 or 9 but also 9.3333. If that's the case I want to have 9.3. How can I do that? If I'm not mistaken "format var1 %4.1f" doesn't change the variable, just how it is displayed.

    Cheers!

  • #2
    Code:
    search precision
    reveals resources -- especially the blog posts by WIlliam Gould -- that explain that trying to hold numbers with decimal parts exactly is doomed to failure unless we are considering numbers that can be held exactly in binary. Thus

    9.5
    9.25
    9.75
    9.125
    9.375
    9.625
    9.875

    (and so on) qualify but not other instances of

    9 + 0.1 k
    9 + 0.01 k
    9 + 0.001 k

    (and so on) within (9, 10),

    In a nutshell, consider an example like

    Code:
    . di %23.18f 9.3
       9.300000000000000711

    Code:
    gen double Grail = round(var1, 0.1)
    is the closest you can get to your Grail (goal).

    Comment


    • #3
      Thanks Nick, the Grail was reached!

      Comment

      Working...
      X