Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to create a custom number format for use with ToString. However if I do the obvious and create a function that calls NumberForm with modified options:

numberform = 
    ExponentFunction -> (If[-5 < # < 5, Null, #]&), 
    NumberFormat -> (If[#3 != "", SequenceForm[#1, "E", #3],#1]&)]&  

It works fine as a function:


but not with ToString:

ToString[1234567890., numberform]  
ToString[1.23457*10^9, Slot[1]&]

Is there some attribute I have to set to make it behave like the built-in forms? Is that even possible? Is there a workaround?

share|improve this question
up vote 2 down vote accepted

You can use TagSet (/:). This attaches the definition to numberform instead of ToString:

numberformFunction = 
  NumberForm[#, ExponentFunction -> (If[-5 < # < 5, Null, #] &), 
    NumberFormat -> (If[#3 != "", 
    SequenceForm[#1, "E", #3], #1] &)] &;
numberform /: ToString[s_, numberform] := numberformFunction@s;
ToString[1234567890., numberform]


share|improve this answer
That's an ingenious idea that I'm happy to have been exposed to, but mostly it reminded me that there was a trick not in the documention of ToString that I once knew and had forgotten: all I had to do in the first place was ToString[numberform[1234567890.]]. Thanks. – Mark Barton Jun 17 '14 at 23:18

Why not simply

numberform = 
   ExponentFunction -> (If[-5 < # < 5, Null, #] &), 
   NumberFormat -> (If[#3 != "", SequenceForm[#1, "E", #3], #1] &)] &






share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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