Trim and Count the Decimals
Python 2, 165 180 bytes
At first I was thinking about writing my first Pyth program, got it to count the digits after the potential comma. But then I got quite annoyed, I don't know how you'd enjoy that language, guess it's just for winning purposes. Anyway here's my solution (edited since it didn't work for large numbers):
def t(i):
o,a='',i
while a[-1]=='0':
a=a[:-1]
while a[0]=='0':
a=a[1:]
if a[-1]=='.':a=a[:-1]
if'.'in a:o=str(len(a)-a.index('.')-1)
else:o='0'
if a!=i:o+=" "+a
print o
In case anybody wants to build on my work in Pyth: ~b@+cz"."" "1Wq@b_1"0"~b<b_1)plrb6
To see where you're at, you might want to insert a p between @+
.
05AB1E, 23 bytes (non-competitive)
Damn, I was so close. Python parses very large floats using scientific notation, so I fixed this bug in the interpreter. However, this was done after the challenge and my submission is therefore non-competitive.
Code:
DÞ'.¡0Üg,\DÞ0Ü'.ÜDrQ_i,
Explanation:
D # Duplicate top of the stack, or input when empty
Þ # Convert to float
'.¡ # Split on '.' (decimal point)
0Ü # Remove trailing zeroes
g # Get the length
, # Output top of the stack (the length)
\ # Discard the top item
D # Duplicate top of the stack
Þ # Convert to float
0Ü # Remove trailing zeroes
'.Ü # Remove trailing dots
D # Duplicate top of the stack
r # Reverse the stack
Q_i, # If not equal, print top of the stack
Uses the ISO 8859-1 encoding.
JavaScript (ES6), 156 162
Edit Fixed bug for '-0' - thx @Fez Vrasta Edit 2 6 bytes saved thx @Neil
It's a mess, but it's 100% string based - no limit due to numeric types
s=>(l=k=p=t=0,[...s].map(c=>++t&&c=='.'?p=t:+c&&(l=t,k=k||t)),m=p>l?p-1:p?l:t,k=k>p&&p?p-2:k-1,r=(s<'0'?'-':'')+s.slice(k,m),(p&&m>p?m-p:0)+(r!=s?' '+r:''))
Less golfed
f=s=>
(
// All values are position base 1, so that 0 means 'missing'
// k position of first nonzero digit
// l position of last non zero digit
// p position of decimal point
// t string length
l=k=p=t=0,
// Analyze input string
[...s].map((c,i)=>c=>++t&&c=='.'?p=t:+c&&(l=t,k=k||t)),
// m position of last digits in output
// if the point is after the last nz digit, must keep the digits up to before the point
// else if point found, keep up to l, else it's a integer: keep all
m=p>l?p-1:p?l:t,
// the start is the first nonzero digit for an integer
// but if there is a point must be at least 1 char before the point
k=k>p&&p?p-2:k-1,
// almost found result : original string from k to m
r=(s<'0'?'-':'')+s.slice(k,m), // but eventually prepend a minus
(p&&m>p?m-p:0) // number of decimal digits
+(r!=s?' '+r:'') // append the result if it's different from input
)
Test
F=s=>(l=k=p=t=0,[...s].map(c=>++t&&c=='.'?p=t:+c&&(l=t,k=k||t)),m=p>l?p-1:p?l:t,k=k>p&&p?p-2:k-1,r=(s<'0'?'-':'')+s.slice(k,m),(p&&m>p?m-p:0)+(r!=s?' '+r:''))
console.log=x=>O.textContent+=x+'\n';
// Test cases
;[['-12.32','2'],['32','0'],['3231.432','3'],['-34.0','0 -34']
,['023','0 23'],['00324.230','2 324.23'],['10','0'],['00.3','1 0.3']
,['0','0'],['-0','0'],['-04.8330','3 -4.833']]
.forEach(t=>{
var i=t[0],k=t[1],r=F(i);
console.log((k==r?'OK ':'KO ')+i+' -> '+r)})
function test(){var i=I.value,r=F(i);R.textContent=r;}
test()
input { width:90% }
input,span { font-family: sans-serif; font-size:14px }
Input: <input id=I oninput='test()' value='-000000098765432112345.67898765432100000'>
Output: <span id=R></span><br>
Test cases<br>
<pre id=O></pre>