# How to convert integer value into real value like 2 into 2.d0 in output of matrix in FortranForm?

matr2 = Table[FortranForm[matr[[i, j]]], {i, 4}, {j, 6}];

matr3 = ExportString[
Flatten@Table[
"matrix(" <> ToString[i] <> "," <> ToString[j] <> ") = " <>
ToString[matr2[[i, j]]], {i, Length[matr2]}, {j,
Length[matr2[[1]]]}], "Table"]

CopyToClipboard[matr3]


I have used the above code to Print out the values from a matrix in a Fortran-like form. The problem is I want to paste the output into a fortran source file, so I need the integer values to appear with ".d0" notation. Can anyone help me with this?

• I am not quite sure that I have got your question right. However, if one multiplies all his data (that is, figures, matrices, variables) by 1.0 , all integer numbers automatically transform into reals, while the rest do not change. May be, this will help? – Alexei Boulbitch Nov 12 '14 at 8:49
• Hii Alexei Boulbitch, Thanks for your help. My question is, I want all integers to be transformed into integer.d0 which is Notation for double precision in Fortran. Any help regarding this?? – Amandeep Nov 12 '14 at 10:57
• how is this different mathematica.stackexchange.com/questions/65328/… – george2079 Nov 12 '14 at 12:48
• @george, that solution given by you is not working for me. – Amandeep Nov 12 '14 at 12:57
• @Amandeep I do not understand. Where do you want to get integer.d0 ? Not in Mma, I guess, since there integer.d0  means a scalar product of a vector integer by another vector d0. Do you mean to export data in such a form? – Alexei Boulbitch Nov 12 '14 at 15:43

You can use NumberForm to customize the output. Something like the following should give you what you're after:

d0Form[precision_:10] := NumberForm[#, precision,
NumberFormat -> (If[(#3 == ""), Row[{#1, "d0", #3}],
Row[{#1, "d", #3}]] &)] &};


Here's a test on a small matrix:

matr = Table[RandomInteger[{0, 10^5}], {i, 2}, {j, 2}];

result = ExportString[
Flatten@Table[
"matrix(" <> ToString[i] <> "," <> ToString[j] <> ") = " <>
ToString@d0Form[matr[[i, j]]], {i, Length[matr]}, {j,
Length[matr[[1]]]}], "Table"]]

CopyToClipboard[result]

(* matrix(1,1) = 78467d0
matrix(1,2) = 51727d0
matrix(2,1) = 33535d0
matrix(2,2) = 85963d0 *)