[Question]
  [
Writing out numbers is among the Hello worlds of programming, often the numbers 1-10.
I want to write out many numbers! Many, Many numbers. But how many numbers do I have to write?
# Task
Given an integer input, give a number as output that would give me the number of digits that would be in a string containing all integer numbers in the range from 0 to the input, inclusive. The negation identifier ("-") counts as a single character.
# Example I/Os
Input: 8  
Written out: 0,1,2,3,4,5,6,7,8  
Output: 9
Input: 101  
written out: 0,1,2,3....,99,100,101  
Output: 196
Input: 102  
written out: 0,1,2,3....,100,101,102  
output: 199
Input -10  
Written out: 0,-1,-2,-3,-4,-5,-6,-7,-8,-9,-10  
output: 22
This is a [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"). The lowest number of bytes wins!
  
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 3 bytes
Code:
```
√ùJg
```
Uses the **CP-1252** encoding. [Try it online!](https://tio.run/nexus/05ab1e#@394rlf6//@GBoYA "05AB1E ‚Äì TIO Nexus")
Explanation:
```
√ù     # Range [0 .. input]
 J    # Join into one string
  g   # Get the length of the string
```
[Answer]
# [Python 2](https://docs.python.org/2/), ~~55~~ 46 bytes
```
lambda n:len(`range(abs(n)+1)`)+2*~n+3*n*(n<0)
```
**[Try it online!](https://tio.run/nexus/python2#HcsxDsIwDEDRGU5hdbKTgOJ2QRHchKGuSFCl1qA0WwVXDy3Lf9NPt3udZB4eAhqmqNhn0WdEGRZUskw92dZ81XZGDerVU02vDCUuBUYFvDhgz3taByf2FI6Hdx61QLP60H0CrLzRnLdrloL76CD9Jar1Bw)**
Getting better.
[Answer]
# [R√∂da](https://github.com/fergusq/roda), 23 bytes
```
f x{[#([seq(0,x)]&"")]}
```
[Try it online!](https://tio.run/nexus/roda#@5@mUFEdrawRXZxaqGGgU6EZq6akpBlb@z83MTNPoZqLM9pCR8HQwBBEGOko6BoaxCrUKKQpxAPJgqLMvBKFeK7a/wA "R√∂da ‚Äì TIO Nexus")
Explained:
```
f x{[#([seq(0,x)]&"")]}
f x{                  } /* Defines function f with parameter x. */
        seq(0,x)        /* Creates a stream of numbers from 0 to x. */
       [        ]       /* Creates an array. */
                 &""    /* Joins with "". */
     #(             )   /* Calculates the length of the resulting string. */
    [                ]  /* Returns the value. */
```
[Answer]
# [Python 2](https://docs.python.org/2/), 41 bytes
```
f=lambda n:len(`n`)+(n and f(n+cmp(0,n)))
```
[Try it online!](https://tio.run/nexus/python2#FYxNCoAgGAXXdYq3kT7JQGsTQZ1Fy4SgXiHdv5/NzGrmSeMejjkGcNhXiqfXtRCBEUlYL8cl1lBr/aQzg9iI3sBZ96M1aJwdyuLKG29UqotoJnyqoCA0/@NLXw "Python 2 ‚Äì TIO Nexus")
[Answer]
# Bash + OS X (BSD) utilities, ~~24~~ 22 bytes
*Thanks to @seshoumara for saving 2 bytes.*
```
seq 0 $1|fold -1|wc -l
```
Test runs on Mac OS X:
```
$ for n in 8 101 102 -10 -1 0 1; do printf %6d $n; ./digitcount $n; done
     8       9
   101     196
   102     199
   -10      22
    -1       3
     0       1
     1       2
```
---
Here's a GNU version:
# Bash + coreutils, ~~40~~ 38 bytes
*Again, 2 bytes saved thanks to @seshoumara.*
```
(seq $1 0;seq 0 $1)|uniq|fold -1|wc -l
```
[Try it online!](https://tio.run/nexus/bash#@69RnFqooGKoYGANYhgAmZo1pXmZhTVp@TkpCrqGNeXJCro5////NzQwAgA "Bash ‚Äì TIO Nexus")
[Answer]
# Python 2, ~~83~~ ,~~78~~ 64 bytes
shortest version:
```
lambda x:sum(map(len,map(str,(range(0,x+cmp(x,.5),cmp(x,.5))))))
```
this version saved 5 bytes, thanks to @numbermaniac :
```
x=input()
print len(''.join(map(str,(range(x+1)if x>0 else range(0,x-1,-1)))))
```
[Try it online!](https://tio.run/nexus/python2#S7ON@V9hm5lXUFqioclVUJSZV6KQk5qnoa6ul5WfmaeRm1igUVxSpKNRlJiXnqpRoW2omZmmUGFnoJCaU5yqABE10KnQNdTRNdQEgf//dQ0NAA "Python 2 ‚Äì TIO Nexus")
this one I came up with on my own after that (same amount of bytes):
```
x=input()
print sum(map(len,map(str,(range(x+1)if x>0 else range(0,x-1,-1)))))
```
[Try it online!](https://tio.run/nexus/python2#S7ON@V9hm5lXUFqioclVUJSZV6JQXJqrkZtYoJGTmqcDootLinQ0ihLz0lM1KrQNNTPTFCrsDBRSc4pTFSCiBjoVuoY6uoaaIPD/v66hAQA "Python 2 ‚Äì TIO Nexus")
[Answer]
# Java 7, 74 bytes *(recursive - including second default parameter)*
```
int c(int r,int n){r+=(n+"").length();return n>0?c(r,n-1):n<0?c(r,n+1):r;}
```
**Explanation (1):**
```
int c(int r, int n){     // Recursive method with two integer parameters and integer return-type
                         // Parameter `r` is the previous result of this recursive method (starting at 0)
  r += (n+"").length();  //  Append the result with the current number's width
  return n > 0 ?         //  If the input is positive
     c(r, n-1)           //   Continue recursive method with n-1
    : n < 0 ?            //  Else if the input is negative
     c(r, n+1)           //   Continue recursive method with n+1
    ?                    //  Else (input is zero)
     r;                  //   Return the result
}                        // End of method
```
---
# Java 7, ~~81~~ 79 bytes *(loop - single parameter)*
If having a default second parameter as `0` for this recursive approach isn't allowed for some reason, a for-loop like this could be used instead:
```
int d(int n){String r="x";for(int i=n;i!=0;i+=n<0?1:-1)r+=i;return r.length();}
```
**Explanation (2)**
```
int d(int n){                 // Method with integer parameter and integer return-type
  String r = "x";             //  Initial String (with length 1 so we won't have to +1 in the end)
  for(int i=n; i != 0;        //  Loop as long as the current number isn't 0
      i += n < 0 ? 1 : 1)     //   After every iteration of the loop: go to next number
    r += i;                   //   Append String with current number
                              //  End of loop (implicit / single-line body)
  return r.length();          //  Return the length of the String
}                             // End of method
```
---
[Answer]
# Mathematica, ~~48~~ ~~47~~ 46 bytes
-1 byte thanks to [Martin Ender](https://codegolf.stackexchange.com/users/8478/martin-ender)!
```
StringLength[""<>ToString/@Range[0,#,Sign@#]]&
```
Anonymous function, taking the number as an argument.
### Shorter solution by [Greg Martin](https://codegolf.stackexchange.com/users/56178/greg-martin), 39 bytes
```
1-#~Min~0+Tr@IntegerLength@Range@Abs@#&
```
[Answer]
# [RProgN 2](https://github.com/TehFlaminTaco/RProgN-2), 5 bytes
```
n0R.L
```
## Explination
```
n0R   # A Stack of all numbers between 0 and the input converted to a number.
   .L # The length of the stringification of this.
```
Simple solution, works like a charm.
[Try it online!](https://tio.run/nexus/rprogn-2#@59nEKTn8///f11DAwA "RProgN 2 ‚Äì TIO Nexus")
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), 5 bytes
```
‚ü¶·π°·µêcl
```
[Try it online!](https://tio.run/nexus/brachylog2#@/9o/rKHOxc@3DohOef//3hDg/9RAA "Brachylog ‚Äì TIO Nexus")
Builds the range [0,input], converts each number to string, concatenates into a single string and returns the length of the result
[Answer]
### PHP, ~~59~~ 60 bytes
**Outgolfed by Roberto06 - <https://codegolf.stackexchange.com/a/112536/38505>**
Thanks to [roberto06](https://codegolf.stackexchange.com/users/64847/roberto06) for noticing the previous version didn't work for negative numbers.
Simply builds an array of the numbers, puts it to a string, then counts the digits (and minus sign)
```
<?=preg_match_all("/\-|\d/",implode(",",range(0,$argv[1])));
```
Run example: `php -f 112504.php 8`
* [8 test case](https://eval.in/752102)
* [101 test case](https://eval.in/752103)
* [102 test case](https://eval.in/752104)
* [-10 test case](https://eval.in/752100)
[Answer]
# [Haskell](https://www.haskell.org/), ~~39~~ 38 bytes
```
f 0=1
f n=length$show=<<[0..n]++[n..0]
```
[Try it online!](https://tio.run/nexus/haskell#@5@mYGBryJWmkGebk5qXXpKhUpyRX25rYxNtoKeXF6utHZ2np2cQ@z83MTNPwVahoCgzr0RBRSFNQ9fQQPM/AA "Haskell ‚Äì TIO Nexus") Edit: saved 1 byte thanks to @xnor!
**Explanation:**
In Haskell for numbers `a` and `b` `[a..b]` is the range from `a` to `b` in 1-increments or 1-decrements, depending on whether `b` is larger `a`. So for a positive `n` the first list in `[0..n]++[n..0]` is `[0,1,2,...,n]` and the second one is empty. For negative `n` the second range yields `[0,-1,-2,...,n]` and the first one is empty. However if `n=0` both ranges yield the list `[0]`, so the concatenation `[0,0]` would lead to a false result of `2`. That's why `0` is handled as a special case. 
The `=<<`-operator on a list is the same as `concatMap`, so each number is converted into a string by `show` and all those strings are concatenated in one long string of which the `length` is finally returned.
---
Before xnor's tip I used `[0,signum n..n]` instead of `[0..n]++[n..0]`.
 `signum n` is `-1` for negative numbers, `0` for zero and `1` for positive numbers and a range of the form `[a,b..c]` builds the list of numbers from `a` to `c` with increment `b`. Thereby `[0,signum n..n]` builds the range `[0,1,2,...,n]` for positive `n` and `[0,-1,-2,...,n]` for negative `n`. For `n=0` it would build the infinite list `[0,0,0,...]` so we need to handle `0` as a special case, too.
[Answer]
# PHP, ~~41~~ 35 bytes
*Saved 6 bytes thanks to user59178*
Since [∞·µà's answer](https://codegolf.stackexchange.com/questions/112504/how-much-do-i-have-to-write/112526#112526) was wrong for a negative input, I took it upon myself to build a new solution : 
```
<?=strlen(join(range(0,$argv[1])));
```
This function : 
* Builds an array from `0` to `$argv[1]` (aka the input)
* Implodes it with an empty character (i.e. transforms it to a string)
* Echoes the length of the string
[Try it here!](https://eval.in/752325)
[Answer]
# [Ruby](https://www.ruby-lang.org/), ~~20 26~~  29 bytes
```
->x{[*x..-1,0,*1..x]*''=~/$/}
```
[Try it online!](https://tio.run/nexus/ruby#@59mq2tXUR2tVaGnp2uoY6CjZainVxGrpa5uW6evol/LxVWgkBZtaGAUC2boGhpAGAax//8DAA "Ruby ‚Äì TIO Nexus")
[Answer]
# R, ~~26~~ 20 bytes
`sum(nchar(0:scan()))`
Very basic approach:
* Make a vector 0:x
* Count the characters in each value (will be coerced to a string automatically)
* Sum
~~Not sure if there are any tricks to cut down the function definition?~~
6 bytes saved thanks to Giuseppe, by taking input from stdin instead.
[Answer]
## Batch, 110 bytes
```
@set/a"n=%1,t=n>>31,n*=t|1,t=1-t*n,i=0
@for /l %%i in (0,1,9)do @set/a"t+=(i-n)*(i-n>>31),i=i*10+9
@echo %t%
```
Computes `sum(min(0,abs(n)+1-10^k),k=0..9)+(n<0?1-n:1)`. (I only have to go up to `9` because of the limitations of Batch's integer arithmetic.)
[Answer]
# [Python 2](https://docs.python.org/2/), 68 bytes
```
def f(i,j=1):
 if i==0:print j
 else:j+=len(`i`);f((i-1,i+1)[i<0],j)
```
[Try it online!](https://tio.run/nexus/python2#JcXBCoAgDADQu1@x44YKrlNYfkkEHnIwEYnq/43oXd44ioCgupqYogEV0JRCPC/tD1QDpd0lVpta6Zg10yKI6tmpZdp0DburNARnMoIc@G/68hzIjBc "Python 2 ‚Äì TIO Nexus")
Longer than but different from other Python solutions. Defines a recursive function called as e.g. `f(10)`
[Answer]
# [MATL](https://github.com/lmendo/MATL), 11 bytes
```
0hSZ}&:VXzn
```
[Try it online!](https://tio.run/nexus/matl#@2@QERxVq2YVFlGV9/@/oYEhAA "MATL ‚Äì TIO Nexus")
```
0h           % Implicitly input n. Append a 0: gives array [n 0]
  S          % Sort
   Z}        % Split array: pushes 0, n or n, 0 according to the previous sorting
     &:      % Binary range: from 0 to n or from n to 0
       V     % Convert to string. Inserts spaces between numbers
        Xz   % Remove spaces
          n  % Length of string. Implicit display
```
[Answer]
# [PowerShell](https://github.com/TryItOnline/TioSetup/wiki/Powershell), 23 bytes
```
-join(0.."$args")|% Le*
```
[Try it online!](https://tio.run/nexus/powershell#@6@blZ@Zp2Ggp6ekkliUXqykWaOq4JOq9f//f0MDIwA "PowerShell ‚Äì TIO Nexus") (will barf on TIO for very large (absolute) inputs)
Uses the `..` range operator to construct a range from `0` to the input `$args` (cast as a string to convert from the input array). That's `-join`ed together into a string (e.g., `01234`) and then the `Le`ngth is taken thereof. That is left on the pipeline and output is implicit.
[Answer]
# [Perl¬†6](https://perl6.org), 18 bytes
```
{chars [~] 0...$_}
```
[Try it](https://tio.run/nexus/perl6#Ky1OVSgz00u25sqtVFBLzk9JVbD9X52ckVhUrBBdF6tgoKenpxJf@z8tv0hBRcvTT688vyilWC83sUBDW0tToZpLQaE4sVIBrFElnqv2v4WCoYEhEBsp6BoaAAA "Perl 6 ‚Äì TIO Nexus")
## Expanded:
```
{  # bare block lambda with implicit parameter ÔΩ¢$_ÔΩ£
  chars        # how many characters (digits + '-')
    [~]        # reduce using string concatenation operator &infix:<~>
      0 ... $_ # deductive sequence from 0 to the input
}
```
[Answer]
## [QBIC](https://drive.google.com/drive/folders/0B0R1Jgqp8Gg4cVJCZkRkdEthZDQ), 25 bytes
```
:[0,a,sgn(a)|A=A+!b$]?_lA
```
Explanation:
```
:[0,a     Read 'a' from the cmd line, start a FOR loop from 0 to 'a'
,sgn(a)|  with incrementer set to -1 for negative ranges and 1 for positive ones
A=A+!b$   Add a string cast of each iteration (var 'b') to A$
]         NEXT
?_lA      Print the length of A$
```
[Answer]
## JavaScript, 50 bytes
*Collaborated with @ETHproductions*
```
n=>{for(t="";n;n<0?n++:n--)t+=n;alert(++t.length)}
```
[Answer]
# Bash + GNU coreutils, 28
```
eval printf %s {0..$1}|wc -c
```
[Try it online](https://tio.run/nexus/bash#DcNNCoAgEAbQfaf4FrYcmWkVdJrwB4QYQ6UW1dnNBy/mAkVSrBCWcQEJj2DIBp8nDDU0EMFoD9d@4CxJW8Rc8bC1Rr73diDXfdbQfw).
[Answer]
# [Retina](https://github.com/m-ender/retina), 28 bytes
```
\d+
$*
1
$`1¬∂
1+
$.&
^-?
0
.
```
[Try it online!](https://tio.run/nexus/retina#U9VwT/gfk6LNpaLFZcilkmB4aBuXIZCnp8YVp2vPZcCl9/@/BZehgSEQG3HpGhoAAA "Retina ‚Äì TIO Nexus")
### Explanation
```
\d+
$*
```
Convert the number to unary, keeping the sign untouched.
```
1
$`1¬∂
```
Each 1 is replaced by everything up to itself plus a newline. With this we get a range from 1 to n if n was positive, from -1 to n with an additional `-` at the start if it was negative. All numbers are in unary and separed by newlines.
```
1+
$.&
```
Convert each sequence of ones to the corresponding decimal number.
```
^-?
0
```
Put a `0` at the start, replacing the extra `-` if it's there.
```
.
```
Count the number of (non-newline) characters.
[Answer]
# Emacs, 20 bytes
```
C-x ( C-x C-k TAB C-x ) M-{input} C-x e C-x h M-=
```
The command itself is 20 keystrokes, but I need clarification on how this should be counted as bytes. I reasoned that counting each keystroke as 1 byte would be the most fair. The command above is written conventionally for easier reading.
## Explanation
```
C-x (
```
Begin defining a keyboard macro. 
```
C-x C-k TAB
```
Create a new macro counter. Writes `0` to the buffer; the counter's value is now 1.
```
C-x )
```
End keyboard macro definition. 
```
M-{input} C-x e
```
After hitting META, type in your input number. The `C-x e` then executes the macro that many times.
```
C-x h
```
Set mark to beginning of buffer (which selects all of the text thus generated).
```
M-=
```
Run character count on the selected region. The number of characters will be printed in the minibuffer. 
## Example
Apologies for the terrible highlighting color. This is an example of using this command with the input 100. The output is in the minibuffer at the bottom of the screen.
[![Example execution with input of 100](https://i.stack.imgur.com/F28O6.png)](https://i.stack.imgur.com/F28O6.png)
[Answer]
# Lua, 52 bytes
```
t=0;for i=0,io.read()do t=t+#tostring(i)end;print(t)
```
Iterates through a for loop from 0 - input, converts the integer `i` to a string and adds the length of the string to `t` before printing `t`
[Answer]
# [Perl 5](https://www.perl.org/), 32 bytes
31 bytes of code + `-p` flag.
```
$\+=y///c for$_>0?0..$_:$_..0}{
```
[Try it online!](https://tio.run/nexus/perl5#U1bULy0u0k/KzNMvSC3KUdAt@K8So21bqa@vn6yQll@kEm9nYG@gp6cSb6USr6dnUFv9/7@hgREA "Perl 5 ‚Äì TIO Nexus")
[Answer]
# C#, ~~77~~ 73 bytes
*-4 bytes thanks to @Kevin Cruijssen*
Lambda function:
```
(r)=>{var l="";for(int i=0,s=r<0?-1:1;i!=r+s;i+=s)l+=i;return l.Length;};
```
Ungolfed and with test cases:
```
class P
{
    delegate int numbers(int e);
    static void Main()
    {
        numbers n = (r) =>
        {
            var l = ""; 
            for (int i = 0, s = r < 0 ? -1 : 1; i != r + s; i += s)
                l += i; 
            return l.Length;
        };
        System.Console.WriteLine(n(8));
        System.Console.WriteLine(n(101));
        System.Console.WriteLine(n(102));
        System.Console.WriteLine(n(-10));
        System.Console.ReadKey();
    }
}
```
[Answer]
## JavaScript, 44 bytes
```
f=(n,i=1)=>n<0?f(-n)-n:n<i?1:n+1-i+f(n,i*10)
```
```
<input type=number oninput=o.textContent=f(+this.value)><pre id=o>
```
[Answer]
## REXX, 56 bytes
```
arg n
l=0
do i=0 to n by sign(n)
  l=l+length(i)
  end
say l
```
]