12
$\begingroup$

I have a big number like: 123 456 789. Now I'll to writ it like this

123 million 456 thousand 789

I've search on the internet but nothing found that works.

How can you do that? Thanks.

$\endgroup$
2
  • 2
    $\begingroup$ Related1, Related2 $\endgroup$ Feb 10, 2015 at 17:32
  • $\begingroup$ To the close voters: the questions are similar but the desired output is different, permitting different approaches. I will not overrule a Close but please consider this. $\endgroup$
    – Mr.Wizard
    Feb 11, 2015 at 20:56

3 Answers 3

13
$\begingroup$

Mathematica 10 introduces IntegerName:

  • IntegerName[n] gives a string containing the full English name of the integer n.

  • IntegerName[n,"type"] gives a string of the specified type.

Possible types include:

  • "DigitsWords" a combination of three-digit numbers and words
  • "Words" using only words
  • "Approximate" the first few digits plus thousands, millions, etc.
  • "ApproximateWords" the first few digits as words plus thousands, etc.

Example:

IntegerName[123456789]
"123 million 456 thousand 789"

ybeltukov comments that small numbers are converted to words:

IntegerName[84]
"eighty‐four"

If this is a problem one could use:

fn = If[Abs[#] < 1000, IntegerString, IntegerName][#] &;

fn /@ {51, 781, 25483}
{"51", "781", "25 thousand 483"}
$\endgroup$
7
  • $\begingroup$ Hah this is great I love it. Should make an expression-to-English translator. "E to the i theta tensor product etc..." $\endgroup$
    – SubSevn
    Feb 10, 2015 at 18:50
  • $\begingroup$ It exists in audio: Speak[Exp[I \[Theta]]] or Speak[BarChart[{3, 4, 5}]] and turn up your speakers. $\endgroup$ Feb 10, 2015 at 19:42
  • $\begingroup$ And in text: SpokenString[Exp[I \[Theta]]] or SpokenString[BarChart[{3,4,5}]]. $\endgroup$ Feb 10, 2015 at 19:57
  • $\begingroup$ @DavidG.Stork This is magic. $\endgroup$
    – SubSevn
    Feb 11, 2015 at 11:56
  • $\begingroup$ Do you know how to deal with shot numbers? IntegerName[123, "DigitsWords"] returns one hundred twenty-three. I'm not sure that it's desired output. $\endgroup$
    – ybeltukov
    Feb 11, 2015 at 23:55
7
$\begingroup$

You people with your fancy version 10 have it too easy.

 StringJoin@Reverse@MapIndexed[ToString[#] <>
       {"", " thousand ", " million ", " billion ", " trillion" }[[First@#2]] &, 
    Reverse@(FromDigits /@ 
      Partition[PadLeft[#, 3 - Mod[#, 3, 1] + # &@Length@#] , 3])] &@   
         IntegerDigits[123456789]

"123 million 456 thousand 789"

$\endgroup$
1
  • $\begingroup$ As a long-time version 7 user I always like seeing backward-compatible answers. +1 $\endgroup$
    – Mr.Wizard
    Feb 10, 2015 at 18:57
4
$\begingroup$

In version 9 and earlier you can do it with Riffle and IntegerDigits[...,1000]:

name[n_Integer] := If[n < 0, "minus ", ""] <> Riffle[ToString /@ IntegerDigits[n, 1000], 
     {" thousand ", " million ", " billion ", " trillion "}, {-2, 2, -2}]

{#, name@#} & /@ ((-12)^Range@10) // TableForm

enter image description here

$\endgroup$
5
  • $\begingroup$ Great! It would be even better if this handled negative numbers. $\endgroup$
    – Mr.Wizard
    Feb 12, 2015 at 7:03
  • 1
    $\begingroup$ @Mr.Wizard I just added a prefix depending on the sign $\endgroup$
    – ybeltukov
    Feb 12, 2015 at 11:52
  • $\begingroup$ nice, i knew there was a better way than what i cooked up. $\endgroup$
    – george2079
    Feb 12, 2015 at 12:23
  • $\begingroup$ I notice you chose "minus" whereas IntegerName uses "negative" -- is the latter an American English thing? p.s. I think it would be arguably better to put the ToString /@ inside Riffle. $\endgroup$
    – Mr.Wizard
    Feb 12, 2015 at 12:23
  • 1
    $\begingroup$ May be. My knowledge of English is not enough. There are some discussions: Math.SE, MO.SE. Of course, ToString of String looks strange :-) $\endgroup$
    – ybeltukov
    Feb 12, 2015 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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