0

Maybe my wording is not clear so I am trying to explain by example:

  • if I have a double like 123.4567 I would like to format it as 123
  • if I have a double like 12.34567 I would like to format it as 12.3
  • if I have a double like 1.234567 I would like to format it as 1.23

Of course I can do this with a switch/case statement but I am wondering if is there any built in or utility formatting, what can do this out of the box.

Thanks in advance

g.pickardou
  • 32,346
  • 36
  • 123
  • 268
  • Just to be sure: if you have a double like 12345.67, you would like to format it as 12300? – JoriO Oct 07 '14 at 12:17
  • I think he means that 12345.67 should be 123. – Godspark Oct 07 '14 at 12:19
  • If 12345.67 should be 123, then I don't understand the assignment. – JoriO Oct 07 '14 at 12:20
  • Thanks for the questions, good point. My number is always between 0 and 999 – g.pickardou Oct 07 '14 at 12:21
  • 2
    Does the "G" format specifier not do what you want? – Dave Mackersie Oct 07 '14 at 12:21
  • @Dave, no it does not. – g.pickardou Oct 07 '14 at 12:24
  • @g.pickardou, I once wrote a solution to quite similar formatting problem that you have, and I couldn't find anything ready. If I remember it right, I first multiplied the double to have the decimal point at appropriate location, then casted it to int, casted back to double and divided so that the decimal point was at original location. – JoriO Oct 07 '14 at 12:30
  • @g.pickardou - do you have an example of the "G" format specifier not working (using "G3")? The only thing I could think of is that you have larger numbers that end up in scientific notation but your numbers are between 0 and 999... – petelids Oct 07 '14 at 12:31
  • @Dave has right, G has something to do with the solution. – g.pickardou Oct 07 '14 at 12:35

3 Answers3

4

Use G3 format specificator:

  String result1 = 123.4567.ToString("G3");
  String result2 = 1.234567.ToString("G3");

Or via String.Format:

  String result = String.Format("{0:G3}", 12.3456789);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • Yes is is, except: Convert.ToDouble(String.Format("{0:G3}", number)).ToString() to prevent getting exponential form. – g.pickardou Oct 07 '14 at 12:34
0

This is a java code can be used for C# as well i guess..

   public static double round(double value) {
       int places = 0;
       BigDecimal bd = new BigDecimal(value);
       if(value>=100){
           places=0;
       }else if(value<=100 && value>=10){
           places = 1;
       }else{
           places = 2;
       }
        bd = bd.setScale(places,RoundingMode.UP);
      return bd.doubleValue();
    }
Kushal Shukla
  • 110
  • 1
  • 7
0

I don't know if there's something simpler, but this should do what you need:

public static string TakeNumDigits(decimal number, int digits, NumberFormatInfo formatProvider = null)
{
    formatProvider = formatProvider ?? NumberFormatInfo.CurrentInfo;
    string num = number.ToString(formatProvider);
    if (digits >= num.Length)
        return num;

    string decSep = formatProvider.NumberDecimalSeparator;
    int decSepIndex = num.IndexOf(decSep);

    if (decSepIndex == -1 || decSepIndex + digits > num.Length)
        return num.Substring(0, digits);
    else
        return num.Substring(0, digits + decSep.Length);
}

Your samples:

decimal num1 = 123.4567m;
string result = TakeNumDigits(num1, 3); // 123
decimal num2 = 12.34567m;
result = TakeNumDigits(num2, 3);        // 12.3
decimal num3 = 1.234567m;
result = TakeNumDigits(num3, 3);        // 1.23
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939