Group Abstract Group Abstract

Message Boards Message Boards

0
|
9.7K Views
|
3 Replies
|
1 Total Like
View groups...
Share
Share this post:

How can Fortran format numbers be output?

Posted 12 years ago

I am trying to output numbers to a file to be read by another program. What I want is always of the format d.dddddddded. I got close using ScientificForm with the NumberFormat option.

fortran[n_] := 
 ScientificForm[n, 9, NumberFormat -> (Row[{#1, "e", #3}] &)]

But there is one issue: When the ScientificForm would have exponent = 0, the exponent (read by #3 in NumberFormat) apparently returns nothing instead of 0, so a number like 1.23 prints outputs the string 1.23e instead of 1.23e0. Any ideas?

Thanks, David

POSTED BY: David Keith
3 Replies
Posted 12 years ago
POSTED BY: David Keith
Posted 12 years ago

I like it. A slight modification, and it works for symbolic constants, too.

f[n_] := ScientificForm[N[n], NumberFormat -> (Row[{#1, "e", If[#3 == "", "0", #3]}] & )]

f[E] = 2.71828e0
POSTED BY: Glenn Carlson
Posted 12 years ago

It's not elegant, but this works.

f[n_] := ScientificForm[n, 
  NumberFormat -> (Row[{#1, "e", If[#3 == "", "0", #3]}] & )]
POSTED BY: David Keith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard