1
$\begingroup$

I want add an index to the file names of my exported images, so I'm using ToString. But I have a problem when my index is a fraction, because then the generated file name includes characters not recognised by my system.

Is it possible to make $2\pi/5$ become, for example, 2Pi--5 or something similar when converting it to a string?

$\endgroup$
6
  • 1
    $\begingroup$ Use Numerator[] and Denominator[] to separate fractions into its components. $\endgroup$ Commented Jun 29, 2015 at 12:28
  • $\begingroup$ Yeah, I knew it would be something simple. Thank you, I guess I should improve my searching skills. $\endgroup$
    – Enzo
    Commented Jun 29, 2015 at 12:30
  • $\begingroup$ Then, you should now be able to answer your own question. :) $\endgroup$ Commented Jun 29, 2015 at 12:31
  • $\begingroup$ Wait, you mean "answer" literally, like edit my post? I'm quite new, sorry if it's a many-time-repeated question... Of course the problem is solved, if that's what you meant :) $\endgroup$
    – Enzo
    Commented Jun 29, 2015 at 12:34
  • 1
    $\begingroup$ StringReplace[SpokenString[2 Pi/5], " " -> "_"] $\endgroup$
    – Kuba
    Commented Jun 29, 2015 at 13:41

1 Answer 1

5
$\begingroup$

You also need InputForm if you want to convert π to Pi.

If you want parentheses, then

2 π/5 // ToString[#, InputForm] & // StringReplace[#, "/" :> "--"] &

"(2*Pi)--5"

or else

2 π/5 // ToString[#, InputForm] & // 
  StringReplace[#, {"/" :> "--", "(" :> "", ")" :> ""}] &

"2*Pi--5"

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.