# GolfScript - The missing function

The GolfScript language has one serious lack, no float or fixed point handling. Remedy this by creating a function F for converting an integer number, given as a string, to a string representing 1 / 10 000 000 000 of it's value. The function must handle both positive and negative values as well as leading 0s.

The following pretty-print rules apply:

No trailing 0s.
No . if the result is integer.
No leading 0s, except for numbers with no integer part, they should have 1 leading 0.
The result must be output as a single string.

Examples:

123 -> 0.0000000123
-00 -> 0
012345678901234567890 -> 1234567890.123456789
-123 -> -0.0000000123
10000000000001 -> 1000.0000000001
123000000000000 -> 12300

The competition is only between GolfScript submissions using no Ruby functionality. Solutions in other languages may be posted, but they are not eligible for winning, these may not use any fixed or float point functionality that the language offers, and must accept arbitrarily large inputs.

### 55 chars

{~.0<"-"*\abs 10.?..3$@%+-1%~)[3-]1$,!!*+-1%@@/\++}:F


Negative numbers are a bit of a pain - responsible for about 13 chars of the 55. Trailing 0s are handled by using a 1 to stand for the decimal point, reversing as a string, converting to an int, and back again, and re-reversing.

• I wrote the requirements pretty strictly, you better give them an extra read and correct your code as needed, I'll postpone a full validation until you make an edit. But it's great to see the question finally being answered. – aaaaaaaaaaaa Apr 6 '11 at 23:28
• @eBusiness, sorry, got so focussed on the pretty print rules that I forgot the bit about a function. – Peter Taylor Apr 7 '11 at 5:54
• Okay, that makes one correct entry of 66 characters, and one incorrect of 57 that prints dots for integers. – aaaaaaaaaaaa Apr 7 '11 at 10:36
• @eBusiness, I'll fix the second one when I got home. Wrote it on a scrap of paper on the bus. Edit: actually, I think I spotted the problem. – Peter Taylor Apr 7 '11 at 11:49
• It pass all my test cases now, time to dig in and see what delicious details are in your code. – aaaaaaaaaaaa Apr 7 '11 at 12:15

### 50 characters

{..~0<<\~abs.,11\-,,0*\++-10/(~.!!\"."+*\+-1%}:F


The most noteworthy trick in this solution is probably -10/, detach the last 10 characters and inverse, so that the trailing 0s can be culled by conversion by conversion to integer.

• I think the nicest trick is the << to get the "-". I spent ages looking for a better way to do that and didn't find yours. – Peter Taylor Apr 12 '11 at 15:14
• -10/ was what gave me the big advantage, though I probably was a bit lucky to find that "Hmmm, it also reverses the string, ohhh, that might be an advantage.". << surely ain't bad either. A shame that we didn't get some more participants, I'm sure this puzzle is hiding more tricks waiting to be uncovered. – aaaaaaaaaaaa Apr 12 '11 at 20:22
• @Nabb Are we missing anything obvious? (And do the summon even work when you haven't participated in the thread?) – aaaaaaaaaaaa Apr 12 '11 at 20:24