2
$\begingroup$

There is a large number

a = 123456789012345678901234567.123456789012345678901234567  

By format with AccountingForm

b = AccountingForm[a, 54]  

it becomes

123456789012345678901234567.123456789012345678901234567  

How to to parameterise the integer part and the fractional and the
n-digit precision part to produce a number. Like

int=123456789012345678901234567.  
fra=.123456789012345678901234567  
pre=54  

resulting to the same outcome like b above?

UPDATE

A general solution was found.

int = 1234567890123456789012345678900;
fra = 123456789012345678901234567890;

SetPrecision[ToExpression[ToString[int] <> "." <> ToString[fra]], 
StringLength[ToString[int]] + StringLength[ToString[fra]]]  

is still a number,

1.234567890123456789012345678900123456789012345678901234567890*10^30  

now look at it as a string with all digits minutely preserved

ScientificForm[%, ExponentStep -> StringLength[ToString[int]]]  

1234567890123456789012345678900.123456789012345678901234567890  
$\endgroup$
1
  • 1
    $\begingroup$ I wonder if this would be useful to you. $\endgroup$
    – dearN
    Jan 12, 2013 at 18:57

3 Answers 3

1
$\begingroup$

I'm not sure I get you exactly, but if you want a list of integer digits from a Real number you can use RealDigits:

 {int, fra} = RealDigits[a, 10, 54] // {#[[1]][[1 ;; #[[2]]]], #[[1]][[#[[2]] + 1 ;;]]} &

If you then need them as numbers as you posted you can use

 FromDigits[int]
 FromDigits[fra] 10^-Length[fra] // N

123456789012345678901234567

0.1234567890123457

$\endgroup$
1
  • $\begingroup$ You want to produce a couple of numbers. Like the one shown. . .You like to build this numbers from parameters as shown. . .So you get this kind of long numbers as shown. $\endgroup$ Jan 13, 2013 at 16:28
1
$\begingroup$

I think you're having precision problems:

a   = 123456789012345678901234567.123456789012345678901234567 ;
int = 123456789012345678901234567. ;
fra = .123456789012345678901234567 ;
pre = 54 ;

Precision /@ {a, int, fra, int + fra}

{53.0915, 26.0915, 26.0915, 26.0915}

Note that a has much higher precision than int + fra. You can use Round to establish int as a proper Integer:

Precision[ Round[int] + fra ]

53.0915

Therefore use:

AccountingForm[Round[int] + fra, pre]

123456789012345678901234567.12345678901234567890123457

$\endgroup$
1
  • 1
    $\begingroup$ OK. . .I am quit lucky whit this. . .The last fraction digit is not as expected. It is 57 here but should be a 567. $\endgroup$ Jan 13, 2013 at 16:34
1
$\begingroup$

I use the routine below to convert long numbers to strings to store them on a database in a simple string format. ToExpression can be used to convert the strings back to numbers when needed.

longform[num_] := Module[{string, split, exp, sig, len},
  If[IntegerQ[num], ToString[num],
   (* Construct string according to InputForm *)
   string = ToString[InputForm[num]];
   If[StringFreeQ[string, "*"], string,
    split = StringSplit[string, {".", "`", "*"}];
    exp = ToExpression[StringDrop[Last[split], 1]];
    sig = StringJoin[Part[split, 1], Part[split, 2]];
    len = StringLength[Part[split, 2]];
    If[exp < 0, If[num < 0,
      StringJoin["-0.", ConstantArray["0", -exp - 1], StringDrop[sig, 1]],
      StringJoin["0.", ConstantArray["0", -exp - 1], sig]],
     If[exp > len, StringJoin[sig, ConstantArray["0", exp - len], "."],
      StringInsert[sig, ".", exp + If[num < 0, 3, 2]]]]]]]

a = 123456789012345678901234567.123456789012345678901234567;

ans = longform[a]

123456789012345678901234567.123456789012345678901234567

{int, fra} = {#1 <> ".", "." <> #2} & @@ StringSplit[ans, "."];
Grid[{{"int =", int}, {"fra =", fra}}]
Print[Row[{"pre = ", StringLength[int <> fra] - 2}]]

int = 123456789012345678901234567.

fra = .123456789012345678901234567

pre = 54

$\endgroup$
1
  • $\begingroup$ Looks very terrible frightening. . .Meet it with a good armour. $\endgroup$ Jan 15, 2013 at 8:07

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.