[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
Miranda Kim <mk@mrc.soton.ac.uk> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: AW: Keeping trailing zeros when formatting a decimal |

Date |
Thu, 01 Oct 2009 13:54:51 +0100 |

. display %09.2g 0.5259 .53 but I used: subinstr(string(-0.22, "%9.2g"), ".", "0.",.) which does the job. However I'm still stuck for the trailing zeros...

My little program goes as follows: program def numformat, rclass args num if abs(`num') > 1 { return local num = string(round(`num', 0.01), "%9.2f") } if abs(`num') < 1 & abs(`num') >= 0.0001 { return local num = subinstr(string(`num', "%9.2g"), ".", "0.",.) } if abs(`num') < 0.0001 { return local num = "< 0.0001" } end Martin Weiss wrote:

<>Most of your problems seem to be due to the fact that you are using "g" instead of "f" in your formatting directives. See [U], 12.5 for more info. Leading zeroes can be induced by inserting a zero after the percentage sign. Also note that you do not need to use the -string()- function, as -display- is able to apply a formatting directive on its own, as seen in the last row: ************* di "`=string(-0.000029, "%9.2g")'" di in red "`=string(-0.000029, "%09.2f")'" di "`=string(-0.0000201, "%9.2g")'" di in red "`=string(-0.0000201, "%7.6f")'" di "`=string(-0.000029, "%9.1g")'" di in red "`=string(-0.000029, "%09.1f")'" di in red %09.1f -0.000029 ************* HTH Martin -----Ursprüngliche Nachricht----- Von: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Miranda Kim Gesendet: Donnerstag, 1. Oktober 2009 13:33 An: statalist@hsphsun2.harvard.edu Betreff: st: Keeping trailing zeros when formatting a decimalI would be very grateful for advice on the following basic formattingquestions...To store a number as a string with a format showing 2 significantfigures, I do the following, for example:di "`=string(-0.000029, "%9.2g")'"If the second significant figure is a zero, how can I make sure this isstill displayed?The following produces: . di "`=string(-0.0000201, "%9.2g")'" -.00002 when I want it to display "-0.000020" Also, how can I make sure it displays the zero before the decimal point? Also, why does . di "`=string(-0.000029, "%9.1g")'" -.000029 not show only 1 significant figure? Many thanks for your help. * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/ * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

* * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

**Follow-Ups**:**AW: st: AW: Keeping trailing zeros when formatting a decimal***From:*"Martin Weiss" <martin.weiss1@gmx.de>

**References**:**st: Keeping trailing zeros when formatting a decimal***From:*Miranda Kim <mk@mrc.soton.ac.uk>

**st: AW: Keeping trailing zeros when formatting a decimal***From:*"Martin Weiss" <martin.weiss1@gmx.de>

- Prev by Date:
**st: Linking legend text to the value of a variable** - Next by Date:
**AW: st: AW: constant in the suest command** - Previous by thread:
**st: AW: Keeping trailing zeros when formatting a decimal** - Next by thread:
**AW: st: AW: Keeping trailing zeros when formatting a decimal** - Index(es):

© Copyright 1996–2017 StataCorp LLC | Terms of use | Privacy | Contact us | What's new | Site index |