[Question]
  [
When writing [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") programs, I usually end up using some numeric constants. I always put them in decimal because that's how I think, but I just realized that my language supports other number formats that might let me shorten my code slightly.
# Challenge
Given a non-negative integer less than 2^53-1, decide whether that integer has the shortest representation in:
* Decimal
* Hexadecimal
* Scientific Notation
### Decimal
Since this is the default format of my language, there is no extra notation needed for this format. Every number is represented as usual for decimal.
### Hexadecimal
My languages uses the `0x` prefix for hexadecimal constants. This means that if a number has 4 hexadecimal digits, it will take 6 bytes to represent that number.
### Scientific notation
My language uses the following format for scientific notation:
> 
> [Real base]e[Integer exponent of 10]
> 
> 
> 
For example, `700` would be represented as `7e3`, and `699` would be represented as `6.99e3`, because *the base must be between -10 and 10* (non-inclusive). For the purposes of this challenge, the base will always be at least 0, since the inputted number is non-negative.
# Output
You should return a way of identifying which format is shortest (i.e. 0 for decimal, 1 for hex, 2 for scientific). Alternatively, you may output the smallest representation of the number itself.
# Test cases
```
Decimal       | Hexadecimal  | Scientific        | Winner
--------------|--------------|-------------------|-------------
0             | 0x0          | 0e0               | Decimal
15            | 0xF          | 1.5e1             | Decimal
6999          | 0x1B57       | 6.999e3           | Decimal
7000          | 0x1B58       | 7e3               | Scientific
1000000000000 | 0xE8D4A51000 | 1e12              | Scientific
1000000000001 | 0xE8D4A51001 | 1.000000000001e12 | Hexadecimal
1000000001000 | 0xE8D4A513E8 | 1.000000001e12    | Hexadecimal
1000001000000 | 0xE8D4B45240 | 1.000001e12       | Scientific
```
# Scoring
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the answer in the shortest bytes for each language wins.
  
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 23 bytes
```
hgÌ¹gD<g>¹ÀðìÁ0ÜðÜg+)Wk
```
[Try it online!](https://tio.run/##MzBNTDJM/f8/I/1wz6Gd6S426XaHdh5uOLzh8JrDjQaH5wAZc9K1NcOz//83NAABCGkAAA "05AB1E – Try It Online")
-1 thanks to [Emigna](https://codegolf.stackexchange.com/users/47066/emigna).
`0` for hexadecimal, `1` for decimal, `2` for scientific.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 27 bytes
```
Dg<¹À'.ìÁ0Ü'.Ü…ÿeÿIh…0xÿ)é¬
```
[Try it online!](https://tio.run/##MzBNTDJM/f/fJd3m0M7DDep6h9ccbjQ4PAfImPOoYdnh/amH93tmAFkGFYf3ax5eeWjN//@GRsYmBgYGAA "05AB1E – Try It Online")
**Explanation**
```
D                            # duplicate input, one copy will be used as decimal notation
 g<                          # len(input)-1
   ¹À                        # push input and rotate left
     '.ìÁ                    # prepend a dot and rotate right
         0Ü'.Ü               # remove trailing zeroes and then any trailing dot
              …ÿeÿ           # format scientific notation
                  Ih         # input converted to hex
                    …0xÿ     # format hex
                        )    # wrap in a list
                         é   # sort by length
                          ¬  # get the first (shortest) item
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 28 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
TṀµỊ¬+‘
DµL’DL+Ç,L
b⁴L+2;ÇỤḢ
```
A monadic link returning `1`, `2`, or `3` for hexadecimal, scientific, or decimal respectively.
**[Try it online!](https://tio.run/nexus/jelly#ATcAyP//VOG5gMK14buKwqwr4oCYCkTCtUzigJlETCvDhyxMCmLigbRMKzI7w4fhu6ThuKL///83MDAw "Jelly – TIO Nexus")** or see a [test suite](https://tio.run/nexus/jelly#@x/ycGfDoa0Pd3cdWqP9qGEGl8uhrT6PGma6@Ggfbtfx4Up61LjFR9vI@nD7w91LHu5Y9B/E6H7UMMcjtSIxJTU5MzcxB8gLTs5MzSvJTMtMBnJcYMJzuY7uOdz@qGmNNxBH/v9voKNgaKqjYGZpaamjYG5gAOIbIAFUriES1xBJFir4NS9fNzkxOSMVAA).
*I thought this would be shorter, but I can't see it so am posting.*
### How this monstrosity works...
```
TṀµỊ¬+‘    - Link 1, length of mantissa + "e": list of decimal digits  e.g. [7,0,1,0]
T          - truthy indexes                                                 [1,  3  ]
 Ṁ         - maximum                                                             3
  µ        - monadic chain separation, call that m
   Ị       - insignificant? (abs(m)<=1) -- here: 1 for m=1, 0 otherwise          0
    ¬      - logical not                  i.e. 1 if a "." will be used           1
     +     - add m                                                               4
      ‘    - increment                    always uses an 'e'                     5
DµL’DL+Ç,L - Link 2, lengths of scientific and decimal notations: non-negative-integer, n
D          - cast to decimal list
 µ         - monadic chain separation, call that d
  L        - length of d (number of decimal digits of n)
   ’       - decrement (value of exponent)
    D      - cast to decimal list (exponent's digits)
     L     - length (number of characters in the exponent)
       Ç   - call last link (1) as a monad(d) (number of characters in mantissa + "e")
         L - length of d (number of decimal digits of n)
        ,  - pair
b⁴L+2;ÇỤḢ - Main link: non-negative-integer, n
 ⁴        - literal 16
b         - convert n to base 16
  L       - length (number of hexadecimal digits)
   +2     - add two (number of characters including the "0x")
      Ç   - call the last link (2) as a monad (characters in scientific and decimal)
     ;    - concatenate ([charsInHexadecimal, charsInScientific, charsInDecimal])
       Ụ  - sort indexes by value
        Ḣ - head (1-based-index in the above list of (one of) the shortest)
```
[Answer]
# JavaScript (ES6), 90 bytes
Returns 0 for decimal, 1 for hexadecimal, -1 for scientific.
```
n=>(l=Math.log,D=l(n)/l(10),H=l(n)/l(16)+2,S=n.toExponential().length-1,S<H?-(S<D):+(H<D))
```
```
f=
n=>(l=Math.log,D=l(n)/l(10),H=l(n)/l(16)+2,S=n.toExponential().length-1,S<H?-(S<D):+(H<D))
console.log(
    f(0),
    f(15),
    f(6999),
    f(7000),
    f(1000000000000),
    f(1000000000001),
    f(1000000001000),
    f(1000001000000)
)
```
## Explanation
* `log(n) / log(10)`: base-10 logarithm of `n`; roughly the length of `n` as a decimal.
* `log(n) / log(16) + 2`: base-16 logarithm of `n` plus 2; roughly the length of `n` as a hexadecimal plus the prepended `0x`.
* `n.toExponential().length - 1`: [`n.toExponential()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toExponential) returns a string with `n` in scientific format (e.g. `7e+3`) but we subtract 1 from its length to account for the extraneous `+`.
Now that we have the lengths of all 3 representations `D`, `H`, and`S`, we compare:  
`S<H?-(S<D):+(H<D)`
---
# JavaScript (ES6), 97 bytes
This one outputs the number in the format with the shortest length. Inspired by [@Shaggy's deleted attempt](https://codegolf.stackexchange.com/a/124193/47097).
```
n=>[n+'','0x'+n.toString(16),n.toExponential().replace('+','')].sort((x,y)=>x.length-y.length)[0]
```
```
f=
n=>[n+'','0x'+n.toString(16),n.toExponential().replace('+','')].sort((x,y)=>x.length-y.length)[0]
console.log(
    f(0),
    f(15),
    f(6999),
    f(7000),
    f(1000000000000),
    f(1000000000001),
    f(1000000001000),
    f(1000001000000)
)
```
[Answer]
# C#, ~~106~~ ~~97~~ ~~96~~ ~~143~~ 132 bytes
```
using System.Linq;n=>new[]{n+"",$"0x{n:X}",(n+"").Insert(1,".").TrimEnd('0','.')+"e"+((n+"").Length-1)}.OrderBy(s=>s.Length).First()
```
Annoyingly in C# the `ulong.ToString` format specifier `e` loses precision on the higher numbers so I've had to do it manually. There's probably a shorter way to do it but this works for now. It also formats it incorrectly for this challenge so I would have to manually strip it's output anyway.
If I set a string to the value of `n` as `var s=n+"";` it works out longer because of the explicit return and extra curly braces.
It returns the shortest value from the array of each different value where `[0] = decimal, [1] = hexadecimal, [2] = scientific`.
Full/Formatted version:
```
using System.Linq;
Func<ulong, string> f = n =>
    new[]
    {
        n + "",
        $"0x{n:X}",
        (n + "").Insert(1, ".").TrimEnd('0', '.') + "e" + ((n + "").Length - 1)
    }.OrderBy(s => s.Length).First();
```
The correct way to calculate the scientific output is:
```
(n < 1 ? n + "" : (n + "").Insert(1, ".").TrimEnd('0', '.')) + "e" + ((n + "").Length - 1)
```
However, seeing as `0` is shorter than `0e0` I can remove that special case.
[Answer]
# Python 2, ~~83~~ 77 bytes
Outputs the smallest representation of the number.
```
import re
lambda n:min(`n`,hex(n),re.sub('\.?0*e\+0?','e','%.15e'%n),key=len)
```
[**Try it online**](https://tio.run/##VYpBCoMwFET3OUU2ksTakFjSoiBexIVKvxiq35BGWk@fuijYDszivRm3hXHBPA5VE6du7u8dxXK2yFtssxHeHEXmQT7XnrNG1iqF5qRqljHYm0htgCX75QFbNQEKYme3@EA9xNdoJ6C6dN5ioAO36NbAhYiKaEOuRVGQm1I7qJ/8kT5IH9vXkTxNzeWcfwA)
**Ungolfed:**
```
import re
n=input()
d=`n`
h=hex(n)
s=re.sub('(.)\.?0*e\+0?',r'\1e','%.15e'%n)
print min(d,h,s,key=len)
```
The regex removes trailing zeros and the decimal point if necessary, as well as the plus sign and leading zero from the exponent if there is one.
[Answer]
# [Ohm](https://github.com/MiningPotatoes/Ohm), 35 bytes
```
l┼xl2+┼DRîsRl≥al≤Dla°┼îa/ì\?≥;+WD╤k
```
[Try it online!](https://tio.run/##y8/I/f8/59GUPRU5RtpAyiXo8LrioJxHnUsTgcQSl5zEQxuAwofXJeofXhNjDxS31g53eTR1Sfb//4YGCGAIAA "Ohm – Try It Online")
Outputs 0 for decimal, 1 for hex and 2 for scientific.
Explanation:
```
l                                      Implicit input, get length                                          
 ┼                                     Input again
  x                                    To hex
   l                                   Get length
    2+                                 Add 2 because of "0x"
      ┼                                Get input again
       D                               Duplicate on the stack
        RîsR                           Remove zeroes at the end (reverse, to int, to string, reverse)
            l                          Get length (= length of base)
             ≥                         Add 1 because to count "e" in the scientific notation
              a                        Swap top two values on the stack
               l≤                      Get length - 1 ( = get the exponent of 10 in scientific notation)
                 D                     Duplicate on the stack
                  l                    Get length ( = length of the exponent)
                   a                   Swap. Now on top of the stack we have the exponent again
                    °                  10^exponent
                     ┼î                Get input for the fourth time
                       a/              Divide input by the 10^exp calculated earlier
                         ì\?           If this thing is not an integer...
                            ≥;         ...add one to count the "."
                              +        Sum base length ( + "e") + exponent length ( + ".")
                               W       Wrap stack in array
                                D      Duplicate
                                 ╤k    Get index of min value
```
[Answer]
# [PHP](https://php.net/), 90 bytes
prints 0 for decimal, 1 for hexadecimal and 2 for scientific
in case of a tie the highest number will be print
```
<?=array_flip($m=[$l=log10($a=$argn)^0,2+(log($a,16)^0),strlen(($a/10**$l).$l)])[min($m)];
```
[Try it online!](https://tio.run/##K8go@G9jX5BRwKWSWJSeZ6tkaGhkbGJgYKBkzWVvB5SyTSwqSqyMT8vJLNBQybWNVsmxzclPNzTQUEm0BWvRjDPQMdLWAAoChXQMzYB8TZ3ikqKc1DwNoIi@oYGWlkqOph4Qx2pG52bmAY3RjLX@/x8A "PHP – Try It Online")
# [PHP](https://php.net/), 91 bytes
prints 0 for decimal, 1 for hexadecimal and 2 for scientific
in case of a tie the lowest number will be print
```
<?=array_search(min($m=[$l=log10($a=$argn)^0,2+(log($a,16)^0),strlen(($a/10**$l).$l)]),$m);
```
[Try it online!](https://tio.run/##HcvBCsIwEATQez@j7CFbQ91U6aWu/RBRCVIaIYlh24tfHxcPc5g3TAmlXuYSSgNe1sytc8PpTETt1MxXndiL@O9zW7y8gknvbCDxDSLHz@rIgOf/ER9kh4NRVLJu1I522yUu2agcHXUdROw1d7SQcKr1Bw "PHP – Try It Online")
# [PHP](https://php.net/), 103 bytes
prints 0 for decimal, 1 for hexadecimal and 2 for scientific
in case of a tie all numbers will be print
```
foreach($m=[$l=log10($a=$argn)^0,2+(log($a,16)^0),strlen(($a/10**$l).$l)]as$k=>$v)echo$v-min($m)?"":$k;
```
[Try it online!](https://tio.run/##HczBDsIgEATQu59B9sBW1KUaD1bshxhNSIOlKQVCTX8fNx7mMC@TyT7Xe5993oEtYzRC6/Z8ISLR1U8qzg5ewmKeEExIoyYJ1vyX@CbV7iUjk9JX7qjWbwkuSpaTpqaBgEfOy64wmwds6AafYDssU@RT7IW4wdzV@gM "PHP – Try It Online")
# [PHP](https://php.net/), 109 bytes
Output an array with the shortest solutions
```
for(;!$p=preg_grep("#^.{".++$i."}$#",[$a=$argn,"0x".dechex($a),$a/10**($l=log10($a)^0)."e$l"]););print_r($p);
```
[Try it online!](https://tio.run/##HctBCsIwEADAu79wu4ekLXFjvcXgQ8SWoDEphHZZPBTEt0f0OjCcuZ4vnHmHQdLiwdrjcCIicPW5inJ7ZM8S05QksoJmNG8wXYezgQ820F8x@H/tgTYwj3jPcVMYdI/hYKltFRZf1mTphyNpAxEL3LTTjmVeXpMoZO1q/QI "PHP – Try It Online")
[Answer]
# C, 187185 bytes
```
main(){long long N;scanf("%lli",&N);long long D=log10(N)+1,H=log(N)/log(16)+3,F,S,i=1;while(N>i&&!(N%i))i*=10,F++;S=ceil(log10(D-1))+1+D-F+(D-F>1);printf("%i",N?H>D?2*(D>S):1+(H>S):N);}
```
Decompressed:
```
void main(){
    long long N;
    scans("%lli", &N);
    long long D = log10(N) + 1; // Length of number (decimal)
    long long H = log(N)/log(16) + 3; // Length of number (hexadecimal)
    long long F; // Number of 0s at the end of decimal number
    long long S; // Length of number (scientific notation)
    long long i; // Counter (more or less)
    // Get number of zeros at the end of decimal number
    while(N > i && (N % i) == 0){
        i = i * 10;
        F++;
    }
    S = ceil(log10(D - 1)) + 1 + D - F + (D-F>1); // (Power) + (e) + (multiplier + (1 if len(multiplier) > 1))
    printf("%i", N!=0 ?
                (H > D ? 2 * (D > S) : 1 + (H > S)) 
              : 0); // Print the shortest number
}
```
Prints 0 for decimal, 1 for hex, 2 for scientific notation.
[Answer]
# TI-Basic, 130 bytes
```
Input N:If not(N:Goto 0:1+int(log(N→D:3+int(logBASE(N,16→H:0→F:1→I:While N>I and not(fPart(N/I:10I→I:F+1→F:End:log(D-1→L:1+D-F+(D-F>1):Ans+int(L)+(0≠fPart(L→S:(H>D)2(D>S)+(H≤D)(1+(H>S)→N:Lbl 0:N
```
Or, alternatively:
```
�N>θN>�0>1p��ND>3p�������BASEN+16H>0F>1I>�NlI@��N�I>10II>Fp1F>�>�Dq1L>1pDqFpDqFl1>rp�Lp0o�LS>HlD2DlSpHmD1pHlSN>�0>N
```
Or, in hex:
```
dc4e3eceb84e3ed7303e3170b1c04e04443e3370b1bbbcbbbfbbb642415345104e2b313604483e3004463e3104493ed14e6c4940b8ba4e83493e31304904493e46703104463ed43ec0447131044c3e317044714670104471466c31113e7270b14c117010306fba4c04533e10486c44113210446c53117010486d441110317010486c5311044e3ed6303e4e
```
Prints 0 for decimal, 1 for hex, 2 for scientific notation
]