3
$\begingroup$

I would like to output large integers in scientific notation, e.g., 1000000 as 10^6 and 50000 as 5 x 10^4.

Whereas I know I can calculate this with logarithms and play with NumberFormat, I was wondering whether there is already a defined format for integers.

Note that I do not want to convert the numbers to reals, as then it would look like: 1. x 10^6.

Thank you.

$\endgroup$
2
  • 1
    $\begingroup$ How would 61373 be written? It would be helpful if you can provide information about the use-case for these numbers. $\endgroup$
    – Syed
    Commented Feb 27, 2023 at 7:18
  • $\begingroup$ @Syed, thank you for your reply. Most of the numbers are round, like the examples I supplied. $\endgroup$ Commented Feb 27, 2023 at 7:47

2 Answers 2

3
$\begingroup$

Is there a final, simply method for outputing 10^5 instead of 1x10^5?

There was a problem with the use of NumberPoint ->"" in earlier version.

Here is a solution that does not use ScientificForm at all. As always, there are many other ways to do this in Mathematica. This simply does it by hand. It looks at the digits of the integer, counts all zero from the right to the left direction and uses that information to manually format the numbers using Superscript.

lis = {1000000, 100, 1, 10, 2500, 60300, 1320, 500, 1230, 1550200};

countZerosOnRight[d_Integer] := 
  Module[{digits = Reverse@IntegerDigits[d], numberOfZeros = 0, n},
   Do[If[digits[[n]] == 0, numberOfZeros++, 
     Return[numberOfZeros, Module]], {n, Length[digits]}]
   ];
myFormating[d_Integer] := 
  Module[{digits = IntegerDigits[d], numberOfZeros, numOfDigits},
   numOfDigits = Length@digits;
   numberOfZeros = countZerosOnRight[d];
   If[First[digits] == 1 && numberOfZeros == numOfDigits  - 1,
    Superscript[10, numOfDigits  - 1]
    ,
    FromDigits[
      digits[[1 ;; 
         numOfDigits - numberOfZeros]]] "\[Times]" Superscript[10, 
      ToString@numberOfZeros]
    ]
   ];
myFormating /@ lis

Mathematica graphics

ps. the above assumes all number are positive integers.

If you prefer to get 1 instead of 10^0 for 1 then this version does that, it also handle negative integers as well.

lis = {1000000, -300, 100, -100, 1, -1, 7, 10, 2500, -60300, 1320, 
   500, 12301, -123000, 1550200, 61373};

countZerosOnRight[d_Integer] := 
  Module[{digits = Reverse@IntegerDigits[d], numberOfZeros = 0, n}, 
   Do[If[digits[[n]] == 0, numberOfZeros++, 
     Return[numberOfZeros, Module]], {n, Length[digits]}]];

myFormating[d_Integer] := 
  Module[{digits = IntegerDigits[d], numberOfZeros, numOfDigits, sign,
     result},
   numOfDigits = Length@digits;
   numberOfZeros = countZerosOnRight[d];
   
   If[numberOfZeros == 0, Return[d, Module]];
   
   If[First[digits] == 1 && numberOfZeros == numOfDigits - 1,
     result = Superscript[10, numOfDigits - 1]
    ,
    result = 
     FromDigits[
       digits[[1 ;; 
          numOfDigits - numberOfZeros]]] "\[Times]" Superscript[10, 
       ToString@numberOfZeros]
    ];
   
   If[d < 0, -result, result]
   
   ];
myFormating /@ lis

Mathematica graphics

$\endgroup$
4
  • $\begingroup$ ScientificForm[N[lis]] works too $\endgroup$ Commented Feb 27, 2023 at 8:48
  • $\begingroup$ @UlrichNeumann but that leaves the decimal point in the result which the OP did not want. But using N instead of multiplying by 1.0 works also. But one still needs to add NumberPoint -> "" $\endgroup$
    – Nasser
    Commented Feb 27, 2023 at 8:53
  • $\begingroup$ Thank you both. Is there a final, simply method for outputing 10^5 instead of 1x10^5? (This is actually where things started for me, as I wrote in the first line of my question.) $\endgroup$ Commented Feb 27, 2023 at 9:40
  • $\begingroup$ @Nasser, thank you very much. This is most handy. I guess this also answers my initial question as to whether there is already a defined format ("no"), or this needs to be done by hand ("yes"). $\endgroup$ Commented Feb 27, 2023 at 10:47
1
$\begingroup$

Using SequenceReplace:

(and lots of inspiration from @Nasser's answer)

sint[n_Integer] := 
 ToString@First@
   SequenceReplace[
    IntegerDigits[n], {a__, k : 0 ...} :> 
     StringForm["``````", {"-", "", "+"}[[2 + Sign[n]]], 
      FromDigits[{a}], 
      If[Length@{k} == 0, "", " x 10^" <> ToString@Length@{k}]]
    ]

Using Nasser's example:

ilist = {1000000, -300, 100, -100, 1, -1, 7, 10, 2500, -60300, 1320, 
   500, 12301, -123000, 1550200, 61373};

istr = sint /@ ilist

{"+1 x 10^6", "-3 x 10^2", "+1 x 10^2", "-1 x 10^2", "+1", "-1",
"+7", "+1 x 10^1", "+25 x 10^2", "-603 x 10^2", "+132 x 10^1", "+5 x
10^2", "+12301", "-123 x 10^3", "+15502 x 10^2", "+61373"}

olist = SemanticInterpretation[#] & /@ istr

ilist == olist

True

$\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.