Announcement

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

  • Counting zeros

    Is there a quick way to count the zeros in a number? For example, 500 000 has five zeros.

  • #2
    The precise problem in Stata I guess to be for a numeric variable holding integers only.

    Here is one approach. Documented at http://www.stata-journal.com/sjpdf.h...iclenum=dm0056

    Code:
    clear
    input numeric
    42
    666
    10
    100
    1000
    10000
    end
    
    tostring numeric, generate(string)
    gen nzero = length(string) - length(subinstr(string, "0", "", .))
    
    list, sep(0)
    
         +--------------------------+
         | numeric   string   nzero |
         |--------------------------|
      1. |      42       42       0 |
      2. |     666      666       0 |
      3. |      10       10       1 |
      4. |     100      100       2 |
      5. |    1000     1000       3 |
      6. |   10000    10000       4 |
         +--------------------------+
    If you have a string variable instead, things will be easier. If you want to do this with fractional parts, that's messier.

    Comment

    Working...
    X