1
$\begingroup$

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)?

$\endgroup$
20
  • 1
    $\begingroup$ Is NumberForm[yourNumber, {\[Infinity], 6}, ExponentStep -> \[Infinity]] what you are looking for? $\endgroup$ – Mike Honeychurch Jan 28 '15 at 21:58
  • 2
    $\begingroup$ @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. $\endgroup$ – David G. Stork Jan 28 '15 at 22:01
  • $\begingroup$ @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 $\endgroup$ – Mike Honeychurch Jan 28 '15 at 22:03
  • $\begingroup$ ...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? $\endgroup$ – Mike Honeychurch Jan 28 '15 at 22:06
  • 2
    $\begingroup$ Have you tried CForm? It gives 2.77778e11. $\endgroup$ – Szabolcs Jan 28 '15 at 22:25
3
$\begingroup$

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

$\endgroup$
2
$\begingroup$

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.

$\endgroup$
2
  • 1
    $\begingroup$ my reading of the question was that he wants no exponents $\endgroup$ – Mike Honeychurch Jan 28 '15 at 23:14
  • $\begingroup$ @MikeHoneychurch Oops. $\endgroup$ – Mr.Wizard Jan 29 '15 at 0:07
1
$\begingroup$
$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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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