Message Boards Message Boards

0
|
3563 Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:

[Solved] How to better perform this operation within a mapping?

Posted 3 years ago

Hi folks, In the middle of another mapping: I am performing Round and ToString on #[[2]], and then if that result ends in "." I am appending a "0%" otherwise just "%".

Now to accomplish this I've placed the modification in a List, then used map to force it into the If[] statement, then of course I have to use Part to take it out of a list at the end. How is this done without the use of List?

If[StringPart[#, -1] == ".", # <> "0%", # <> "%"] & /@ {ToString[Round[100*#[[2]], 0.1]]}
POSTED BY: Richard Frost
2 Replies

How is this done without the use of List?

Just

If[StringPart[#, -1] == ".", # <> "0%", # <> "%"] & @ ToString @ Round[100*#[[2]], 0.1]

but consider:

ToString[NumberForm[100 #[[2]], {Infinity, 1}]] <> "%" & /@ 
  {{, 0.0156}, {, 0.01}}

{"1.6%", "1.0%"}

Or

TextString@Quantity[Round[100 #[[2]], .1], "Percent"] & /@ 
  {{,  0.0156}, {, 0.01}}
POSTED BY: Kuba Podkalicki

Thank you Kuba for your quick reply plus examples. So "& @" now makes sense, a prefixed pure function. NumberForm is a good idea too -- it gets to the heart of what I wanted. Quantity[] is also desirable, but upon export in tabular form does not easily import to some programs. Works great in plots though.

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

Group Abstract Group Abstract