Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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  
share|improve this question
1  
I wonder if this would be useful to you. –  drN Jan 12 '13 at 18:57

3 Answers 3

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

share|improve this answer
    
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. –  Hp Radojewski Schäfer Von Jan 13 '13 at 16:28

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

share|improve this answer
1  
OK. . .I am quit lucky whit this. . .The last fraction digit is not as expected. It is 57 here but should be a 567. –  Hp Radojewski Schäfer Von Jan 13 '13 at 16:34

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

share|improve this answer
    
Looks very terrible frightening. . .Meet it with a good armour. –  Hp Radojewski Schäfer Von Jan 15 '13 at 8:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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