Is there a global setting for number form

I’m creating strings which represent equations in C to use with library link. The problem I have is that numbers in ScientificForm are no longer useful after they are converted to strings. For example, 2.77778*10^11 becomes

11
2.77778 10

So is there a way to make mathematica globally treat all numbers as standard form (i.e. non-scientific form)?

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

1

 

Is NumberForm[yourNumber, {\[Infinity], 6}, ExponentStep -> \[Infinity]] what you are looking for?
– Mike Honeychurch
Jan 28 ’15 at 21:58

2

 

@MikeHoneychurch: I believe he wants a GLOBAL setting, like SetOptions[] so he doesn’t have to convert each and every single number in a notebook.
– David G. Stork
Jan 28 ’15 at 22:01

  

 

@DavidG.Stork on the other hand he is creating strings from the numbers therefore he has code converting the global numbers to strings in which case he just needs to use NumberForm in his code
– Mike Honeychurch
Jan 28 ’15 at 22:03

  

 

…to continue, rather than think of the problem as converting your numbers globally and then converting to strings why not make the reformatting part of your string conversion and leave the numbers as they are in the notebook?
– Mike Honeychurch
Jan 28 ’15 at 22:06

2

 

Have you tried CForm? It gives 2.77778e11.
– Szabolcs
Jan 28 ’15 at 22:25

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

3 Answers
3

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

Since you are converting numbers to strings you presumably have some code for doing this? Since you are not globally converting numbers to strings via an option setting you still require the string conversion step in your process. Therefore why not just make the reformatting of your number part of this code?

I think something like this may be what you need. I use similar when exporting numbers to XML.

ToString@NumberForm[2.77778*10^11, {\[Infinity], 6},ExponentStep -> \[Infinity]]

So rather than apply e.g. ToString[#]&/@numbers in your code change it to e.g.

ToString@NumberForm[#, {\[Infinity], 6},ExponentStep -> \[Infinity]]&/@numbers

Edit

Now that we can see how you are doing the string conversion just make a function myString

myString=ToString@NumberForm[#, {\[Infinity], 6},ExponentStep -> \[Infinity]]&

Now simply do a find and replace in your code, Find ToString, replace with myString

The two-line formatting you quote in your question is the result of string conversion into OutputForm, which is the default FormatType of ToString. If you use CForm you get something more useful for C:

ToString[2.77778*10^11, CForm]

“2.77778e11”

You can change the default FormatType with SetOptions:

SetOptions[ToString, FormatType -> CForm];

Now:

ToString[2.77778*10^11]

“2.77778e11”

Since you did not state the method that you use for conversion to strings it is difficult to recommend anything more specific.

1

 

my reading of the question was that he wants no exponents
– Mike Honeychurch
Jan 28 ’15 at 23:14

  

 

@MikeHoneychurch Oops.
– Mr.Wizard♦
Jan 29 ’15 at 0:07

$PrePrint = NumberForm[#, ExponentFunction -> (Null&)] &

$PrePrint is a function that is applied to every expression before display; in this case, we simply use NumberForm to format all numbers in the expression with no exponent.