Group Abstract Group Abstract

Message Boards Message Boards

0
|
6.3K Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

How to use and to set position for variables inside string template?

POSTED BY: Luiz Gutierres
4 Replies

Then try this

Clear[padit];
padit[num_] := Module[{},
Return[
StringTake[StringJoin[StringJoin[Table[" ",{28}]],
StringReplace[ToString[NumberForm[FortranForm[num], 11]],
"e"->"E"]], -28]
]
];
CODIGO=StringTemplate["
   C ----------------------------------------------------------------------
   C [R] Matrix
   C ----------------------------------------------------------------------
   $VINTAGE, 1,
     X0001AX0001X               0.82544479318
     X0001AX0001X`1`
     X0001AX0001X`2`
     X0002AX0002X`3`"][padit[0.825444793182342343242342],padit[0.000002342342342341231231],padit[0.081380777833]]

Returns

"
   C ----------------------------------------------------------------------
   C [R] Matrix
   C ----------------------------------------------------------------------
   $VINTAGE, 1,
     X0001AX0001X               0.82544479318
     X0001AX0001X               0.82544479318
     X0001AX0001X             2.3423423423E-6
     X0002AX0002X              0.081380777833                               "

After a bit of exploring StringTemplate[] and TeplateApply[] you can try

Clear[padit];
padit[num_] := Module[{}, 
    Return[StringTake[StringJoin[StringJoin[Table[" ",{28}]],
    StringReplace[ToString[NumberForm[FortranForm[num],11]],"e"->"E"]],-28]]];
TemplateApply[
StringTemplate["
   C ----------------------------------------------------------------------
   C [R] Matrix
   C ----------------------------------------------------------------------
   $VINTAGE, 1,
     X0001AX0001X               0.82544479318
     X0001AX0001X``
     X0001AX0001X``
     X0002AX0002X``", InsertionFunction -> padit],{0.825444793182342343242342,0.000002342342342341231231,0.081380777833}]

Returns same result as above just a bit clearer.

Good luck

POSTED BY: Hans Michel

Thank you very much for your help, Hans Michel.

The problem is solved!

POSTED BY: Luiz Gutierres

Thank you very much for your help, Hans Michel!

I tried to generalize your solution for different possibilities of numbers. They should look like Figure 1, this is why there is a sequence of PaddedForm(s) and NumberForm(s) below. Numbers should be displayed in a Fortran?like form with 11 precise digits. However, I think it get lost during the different kinds of "Form".

In[205]:= ATESTa := 0.825444793182342343242342
ATESTb := 0.000002342342342341231231
ATESTa11E := 
 PaddedForm[ATESTa, 11, NumberFormat -> (Row[{#1, "E", #3}] &)]
ATESTb11E := 
 PaddedForm[ATESTb, 11, NumberFormat -> (Row[{#1, "E", #3}] &)]
TESTa := ToString[
  NumberForm[ATESTa11E, 26, NumberPadding -> {" ", ""}]]
TESTb := ToString[
  NumberForm[ATESTb11E, 26, NumberPadding -> {" ", ""}]]
CODIGO = StringTemplate["
   C ----------------------------------------------------------------------
   C [R] Matrix
   C ----------------------------------------------------------------------
   $VINTAGE, 1,
     X0001AX0001X               0.82544479318
     X0001AX0001X`1`
     X0001AX0001X`2`"][TESTa, TESTb ]

Out[211]= "
C ----------------------------------------------------------------------
C [R] Matrix
C ----------------------------------------------------------------------
$VINTAGE, 1,
  X0001AX0001X               0.82544479318
  X0001AX0001X 0.82544479318E
  X0001AX0001X 2.3423423423E-6"
POSTED BY: Luiz Gutierres

Try the following:

TEST = ToString[NumberForm[0.82544479318,26, NumberPadding -> {" ",""}]];
CODIGO = StringTemplate["
   C ----------------------------------------------------------------------
   C [R] Matrix
   C ----------------------------------------------------------------------
   $VINTAGE, 1,
     X0001AX0001X               0.82544479318
     X0001AX0001X`1`"][TEST]

Will return

   C ----------------------------------------------------------------------
   C [R] Matrix
   C ----------------------------------------------------------------------
   $VINTAGE, 1,
     X0001AX0001X               0.82544479318
     X0001AX0001X               0.82544479318

I'm not sure this is the full solution, as the column is of length 28, I had to use 26 to get it to align. Is it the length minus sign and decimal point locations?

POSTED BY: Hans Michel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard