How can I convert a fraction to a string that can be used in a file name?

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π/52\pi/5 become, for example, 2Pi–5 or something similar when converting it to a string?

=================

1

 

Use Numerator[] and Denominator[] to separate fractions into its components.
– J. M.♦
Jun 29 ’15 at 12:28

  

 

Yeah, I knew it would be something simple. Thank you, I guess I should improve my searching skills.
– Enzo
Jun 29 ’15 at 12:30

  

 

Then, you should now be able to answer your own question. 🙂
– J. M.♦
Jun 29 ’15 at 12:31

  

 

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 🙂
– Enzo
Jun 29 ’15 at 12:34

1

 

StringReplace[SpokenString[2 Pi/5], ” ” -> “_”]
– Kuba
Jun 29 ’15 at 13:41

=================

1 Answer
1

=================

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”