Skip to main content
added 665 characters in body
Source Link
Neil
  • 184.4k
  • 12
  • 76
  • 290

Charcoal, 8140 bytes

≔I⪪S/θW﹪§θ±²§θ±¹⊞θι≧÷⌊θθ≔Xχ⌈E25⌕⮌X⁰↨§θ¹Iι⁰η¿﹪η§θ¹⪫…θ²θ≧÷⌈Φ⊕⌊θ∧ι¬⌈﹪θιθ¿﹪Xχφ§θ¹⪫θ/⁺÷§θ⁰§θ¹⭆I⁺η﹪×§θ⁰÷η§θ¹η⎇κι.I∕§θ⁰§θ¹ 

Try it online!Try it online! Link is to verbose version of code. Explanation: This

≔I⪪S/θ 

Input the fraction.

≧÷⌈Φ⊕⌊θ∧ι¬⌈﹪θιθ 

Reduce it to its lowest terms.

¿﹪Xχφ§θ¹ 

If the denominator is not a factor of 10¹⁰⁰⁰...

⪫θ/ 

... output as a fraction, otherwise...

I∕§θ⁰§θ¹ 

output as a decimal.

81 bytes for the way overengineered version, which works with arbitrary precision positive integer inputs. I'll write a:

≔I⪪S/θW﹪§θ±²§θ±¹⊞θι≧÷⌊θθ≔Xχ⌈E25⌕⮌X⁰↨§θ¹Iι⁰η¿﹪η§θ¹⪫…θ²/⁺÷§θ⁰§θ¹⭆I⁺η﹪×§θ⁰÷η§θ¹η⎇κι. 

Try it online! Link is to verbose version golfed for the expected inputs laterof code. Explanation:

≔I⪪S/θ 

Input the fraction.

W﹪§θ±²§θ±¹⊞θι≧÷⌊θθ 

Reduce it to its lowest terms.

≔Xχ⌈E25⌕⮌X⁰↨§θ¹Iι⁰η 

Find a power of 10 that the denominator might be a factor of.

¿﹪η§θ¹⪫…θ²/ 

But if it isn't then just output the fraction.

⁺÷§θ⁰§θ¹⭆I⁺η﹪×§θ⁰÷η§θ¹η⎇κι. 

Convert the fraction to arbitrary precision decimal.

Charcoal, 81 bytes

≔I⪪S/θW﹪§θ±²§θ±¹⊞θι≧÷⌊θθ≔Xχ⌈E25⌕⮌X⁰↨§θ¹Iι⁰η¿﹪η§θ¹⪫…θ²/⁺÷§θ⁰§θ¹⭆I⁺η﹪×§θ⁰÷η§θ¹η⎇κι. 

Try it online! Link is to verbose version of code. Explanation: This is the way overengineered version, which works with arbitrary precision positive integer inputs. I'll write a version golfed for the expected inputs later.

≔I⪪S/θ 

Input the fraction.

W﹪§θ±²§θ±¹⊞θι≧÷⌊θθ 

Reduce it to its lowest terms.

≔Xχ⌈E25⌕⮌X⁰↨§θ¹Iι⁰η 

Find a power of 10 that the denominator might be a factor of.

¿﹪η§θ¹⪫…θ²/ 

But if it isn't then just output the fraction.

⁺÷§θ⁰§θ¹⭆I⁺η﹪×§θ⁰÷η§θ¹η⎇κι. 

Convert the fraction to arbitrary precision decimal.

Charcoal, 40 bytes

≔I⪪S/θ≧÷⌈Φ⊕⌊θ∧ι¬⌈﹪θιθ¿﹪Xχφ§θ¹⪫θ/I∕§θ⁰§θ¹ 

Try it online! Link is to verbose version of code. Explanation:

≔I⪪S/θ 

Input the fraction.

≧÷⌈Φ⊕⌊θ∧ι¬⌈﹪θιθ 

Reduce it to its lowest terms.

¿﹪Xχφ§θ¹ 

If the denominator is not a factor of 10¹⁰⁰⁰...

⪫θ/ 

... output as a fraction, otherwise...

I∕§θ⁰§θ¹ 

output as a decimal.

81 bytes for the way overengineered version which works with arbitrary precision positive integer inputs:

≔I⪪S/θW﹪§θ±²§θ±¹⊞θι≧÷⌊θθ≔Xχ⌈E25⌕⮌X⁰↨§θ¹Iι⁰η¿﹪η§θ¹⪫…θ²/⁺÷§θ⁰§θ¹⭆I⁺η﹪×§θ⁰÷η§θ¹η⎇κι. 

Try it online! Link is to verbose version of code. Explanation:

≔I⪪S/θ 

Input the fraction.

W﹪§θ±²§θ±¹⊞θι≧÷⌊θθ 

Reduce it to its lowest terms.

≔Xχ⌈E25⌕⮌X⁰↨§θ¹Iι⁰η 

Find a power of 10 that the denominator might be a factor of.

¿﹪η§θ¹⪫…θ²/ 

But if it isn't then just output the fraction.

⁺÷§θ⁰§θ¹⭆I⁺η﹪×§θ⁰÷η§θ¹η⎇κι. 

Convert the fraction to arbitrary precision decimal.

Source Link
Neil
  • 184.4k
  • 12
  • 76
  • 290

Charcoal, 81 bytes

≔I⪪S/θW﹪§θ±²§θ±¹⊞θι≧÷⌊θθ≔Xχ⌈E25⌕⮌X⁰↨§θ¹Iι⁰η¿﹪η§θ¹⪫…θ²/⁺÷§θ⁰§θ¹⭆I⁺η﹪×§θ⁰÷η§θ¹η⎇κι. 

Try it online! Link is to verbose version of code. Explanation: This is the way overengineered version, which works with arbitrary precision positive integer inputs. I'll write a version golfed for the expected inputs later.

≔I⪪S/θ 

Input the fraction.

W﹪§θ±²§θ±¹⊞θι≧÷⌊θθ 

Reduce it to its lowest terms.

≔Xχ⌈E25⌕⮌X⁰↨§θ¹Iι⁰η 

Find a power of 10 that the denominator might be a factor of.

¿﹪η§θ¹⪫…θ²/ 

But if it isn't then just output the fraction.

⁺÷§θ⁰§θ¹⭆I⁺η﹪×§θ⁰÷η§θ¹η⎇κι. 

Convert the fraction to arbitrary precision decimal.