11
$\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
    $\begingroup$ Related1, Related2 $\endgroup$ – Dr. belisarius Feb 10 '15 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 '15 at 20:56
12
$\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$
  • $\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 '15 at 18:50
  • $\begingroup$ It exists in audio: Speak[Exp[I \[Theta]]] or Speak[BarChart[{3, 4, 5}]] and turn up your speakers. $\endgroup$ – David G. Stork Feb 10 '15 at 19:42
  • $\begingroup$ And in text: SpokenString[Exp[I \[Theta]]] or SpokenString[BarChart[{3,4,5}]]. $\endgroup$ – David G. Stork Feb 10 '15 at 19:57
  • $\begingroup$ @DavidG.Stork This is magic. $\endgroup$ – SubSevn Feb 11 '15 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 '15 at 23:55
6
$\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$
  • $\begingroup$ As a long-time version 7 user I always like seeing backward-compatible answers. +1 $\endgroup$ – Mr.Wizard Feb 10 '15 at 18:57
3
$\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$
  • $\begingroup$ Great! It would be even better if this handled negative numbers. $\endgroup$ – Mr.Wizard Feb 12 '15 at 7:03
  • 1
    $\begingroup$ @Mr.Wizard I just added a prefix depending on the sign $\endgroup$ – ybeltukov Feb 12 '15 at 11:52
  • $\begingroup$ nice, i knew there was a better way than what i cooked up. $\endgroup$ – george2079 Feb 12 '15 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 '15 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 '15 at 12:38

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.