One significant figure in C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mrshrinkray@googlemail.com

    One significant figure in C#

    I feel a bit ashamed asking this after 6 years of C#, but is there a
    format string for significant figures before the decimal place?

    e.g.
    2,354,856:
    1 sf = 2,000,000
    2 sf = 2,400,000
    3 sf = 2,350,000

    {0:G} or g gives me scientific notation, but I'm after the above.

    Thanks
  • Dave Shooter

    #2
    Re: One significant figure in C#

    <mrshrinkray@go oglemail.comwro te in message
    news:9b2430fc-810e-4d39-bafe-4e68dde53d4d@a1 g2000hsb.google groups.com...
    >I feel a bit ashamed asking this after 6 years of C#, but is there a
    format string for significant figures before the decimal place?
    >
    e.g.
    2,354,856:
    1 sf = 2,000,000
    2 sf = 2,400,000
    3 sf = 2,350,000
    >
    {0:G} or g gives me scientific notation, but I'm after the above.
    The following doesn't fully answer your question but might help, it's a
    customer formatter I've cobbled together based on the code at:

    http://msdn.microsoft.com/en-us/libr...tprovider.aspx.

    I believe the rounding need to be implemented in the
    SFFormatProvide r.Format() method to achieve the exact results you're looking
    for but this shouldn't be too difficult.

    class Program
    {
    static void Main(string[] args)
    {
    int val = 2354856;
    Console.WriteLi ne(String.Forma t(
    new SFFormatProvide r(), "{0}", val));
    Console.WriteLi ne(String.Forma t(
    new SFFormatProvide r(), "{0:1}", val));
    Console.WriteLi ne(String.Forma t(
    new SFFormatProvide r(), "{0:2}", val));
    Console.WriteLi ne(String.Forma t(
    new SFFormatProvide r(), "{0:3}", val));
    }
    }

    public class SFFormatProvide r : IFormatProvider , ICustomFormatte r
    {
    public object GetFormat(Type formatType)
    {
    if (formatType == typeof(ICustomF ormatter))
    return this;
    else
    return null;
    }

    public string Format(string format, object arg,
    IFormatProvider formatProvider)
    {
    char[] carr = arg.ToString(). ToCharArray();
    int digits = 1;
    if (!String.IsNull OrEmpty(format) )
    digits = Int32.Parse(for mat);

    for (int i = digits; i < carr.Length; i++)
    {
    carr[i] = '0';
    }
    return String.Format(" {0:n0}",
    Convert.ToInt32 (new String(carr)));
    }
    }

    Regards
    David


    Comment

    • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

      #3
      Re: One significant figure in C#

      mrshrinkray@goo glemail.com wrote:
      I feel a bit ashamed asking this after 6 years of C#, but is there a
      format string for significant figures before the decimal place?
      >
      e.g.
      2,354,856:
      1 sf = 2,000,000
      2 sf = 2,400,000
      3 sf = 2,350,000
      >
      {0:G} or g gives me scientific notation, but I'm after the above.
      I don't think so.

      You will need to code it yourself.

      One way:

      public static string SpecialFormat(i nt v, int sf)
      {
      int k = (int)Math.Pow(1 0, (int)(Math.Log1 0(v) + 1 - sf));
      int v2 = ((v + k/2) / k) * k;
      return v2.ToString("0, 0");
      }

      (the calculation of k can be optimized if speed is critical)

      Arne

      Comment

      Working...