0
$\begingroup$

I am dealing with coordinates of atoms, in which I need to control the number of decimals.

Take two sets of coordinates as an example:

PadRight[PadRight[{{4.440892098500626*^-16, 13.302150202128976`, -7.0545304592155516`},{6.949999999999999`,5.617618119215058`,2.2698604949790777`}},{2,4},1],{2,5},PT]

Above gives:

{{4.44089*10^-16, 13.3022, -7.05453, 1, PT}, {6.95, 5.61762, 2.26986, 1, PT}}

But, as this will be written as a line in a text file later, my target is:

{{0.00000,13.30200,-7.05450,1,PT},{6.95000,5.61760,2.26990,1,PT}}

So, I try this:

PadRight[PadRight[NumberForm[{{4.440892098500626*^-16, 
 13.302150202128976`, -7.0545304592155516`}, {6.949999999999999`, 
 5.617618119215058`, 2.2698604949790777`}}, {5, 5}, ExponentFunction -> (Null &)], {2, 4}, 1], {2, 5}, PT]

What I get is:

{{4.44090*10^(-16),13.30200,-7.05450},{6.95000,5.61760,2.26990},1.00000,1.00000,PT}.

How can I get the target number form in the final output?

$\endgroup$
  • $\begingroup$ Have you tried NumberForm[Round[#, 10.^-5], {5, 5}] & /@ list? $\endgroup$ – Somos Apr 16 at 18:35
  • $\begingroup$ Thank you for your suggestion. But I need to Pad[] integers like 0,1 and element symbols like PT, O into the list. If I pad after NumberForm[], it still does not work. Finally I need to Export[] the list into a .txt file. And it is found that even if the right number form is obtained just before Export[], after exporting the number form is restored to the originally uncontrolled form. Is there a command to execute the number form during the whole process? $\endgroup$ – BaZingo Apr 17 at 3:46
0
$\begingroup$

There are two issues here.

  1. PadRight should accept a list as argument, not a NumberForm object. So, you should put the PadRight calls inside of NumberForm:

    paddedData = PadRight[
        PadRight[
            {
            {4.440892098500626*^-16,13.302150202128976`,-7.0545304592155516`},
            {6.949999999999999`,5.617618119215058`,2.2698604949790777`}
            },
            {2,4},
            1
        ],
        {2,5},
        PT
    ];
    
    NumberForm[paddedData, {5, 5}, ExponentFunction -> (Null&)]
    

    {{0.00000,13.30200,-7.05450,1,PT},{6.95000,5.61760,2.26990,1,PT}}

  2. Exporting to a text file. Contrary to what the documentation says, the export to a text file uses InputForm, not OutputForm, and so explicit NumberForm wrappers will appear:

    ExportString[
        NumberForm[
            paddedData,
            {5, 5},
            ExponentFunction -> (Null&)
        ],
        "Text"
    ]
    

    "NumberForm[{{4.440892098500626*^-16, 13.302150202128976, \ -7.0545304592155516, 1, PT}, {6.949999999999999, 5.617618119215058, \ 2.2698604949790777, 1, PT}}, {5, 5}, ExponentFunction -> (Null & )]"

    One way to work around this is to create a new wrapper that formats NumberForm objects in OutputForm. So:

    Format[myNumberForm[args__], InputForm] := OutputForm[NumberForm[args]]
    

    Then:

    ExportString[
        myNumberForm[
            paddedData,
            {5, 5},
            ExponentFunction -> (Null&)
        ],
        "Text"
    ]
    

    "{{0.00000, 13.30200, -7.05450, 1, PT}, {6.95000, 5.61760, 2.26990, 1, PT}}"

$\endgroup$
  • $\begingroup$ Woll sama, I got your good idea: 1) pad 2)myNumberForm 3)export. Two problems are: (1) I cannot use the ExportString[] for .txt as Wolfram outputs the error as "ExportString::format: /Users/binghu/Desktop/22.txt is not a recognized ExportString format". But I can turn to OpenAppend[] and WriteString[] to solve this problem. (2) I finally need to write the list of lists line by line, like: ATOMS -25.02000,-14.44500,0.00000,1,PT -23.63000,-12.03800,0.00000,1,PT -22.24000,-9.63020,0.00000,1,PT $\endgroup$ – BaZingo Apr 19 at 2:09
  • $\begingroup$ Use Export instead of ExportString. I used ExportString so you could see what the file would look like $\endgroup$ – Carl Woll Apr 19 at 2:11
  • $\begingroup$ Oh!That is right:D $\endgroup$ – BaZingo Apr 19 at 2:14
0
$\begingroup$

I think that the simple modified code

list = {{4.440892098500626*^-16, 13.302150202128976`, -7.0545304592155516`},
        {6.949999999999999`, 5.617618119215058`, 2.2698604949790777`}};

ToString@PadRight[ Map[ToString@NumberForm[Round[#, 10.^-5], {5, 5}] &,
   list, {2}], {2, 5}, {PT, 1}]

which returns the string result

{{0.00000, 13.30200, -7.05450, 1, PT}, {6.95000, 5.61760, 2.26990, 1, PT}}

is what you wanted and you can export the string to a text file.

$\endgroup$
  • $\begingroup$ It works! But I found I have to take the "ToString" off, maybe because later I have to write it line by line as: atoms = Join[ PadRight[Map[ToString@NumberForm[Round[#, 10.^-5], {5, 5}] &, Pt2L, {2}], {2 Length[b], 5}, {PT, 1}], PadRight[Map[ToString@NumberForm[Round[#, 10.^-5], {5, 5}] &, Au3L, {2}], {3 Length[B], 5}, {AU, 2}]] ATOMS = ReplacePart[atoms, Position[atoms, {"0.00000", "0.00000", "0.00000", 1, PT}] -> {0, 0, 0, 0, PT}] file = OpenAppend["x.txt"] string = ExportString[ATOMS, "CSV"]; WriteString[file, string] WriteString[file, "\n"]; Close[file] $\endgroup$ – BaZingo Apr 18 at 12:25
  • $\begingroup$ Great!. I only put it in because I thought you wanted it. $\endgroup$ – Somos Apr 18 at 13:50

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.