2

I need thousand separator in C# code or VB which I will be using in BluePrism code stage. One of the questions in stack overflow gave me the solution upto some extent but not complete.

I am having more than two digits after decimal point and I need the number to have thousand separator and retain all the decimal values.

Out = String.Format("{0:N}%", In);

If In is -137855027.5123456 Out value is -137,855,027.51%, but I need all the values after the decimal point.

monu
  • 211
  • 4
  • 10
  • I believe that the "F" specifier includes group separators where the "N" specifier does not. – jmcilhinney Jul 25 '19 at 07:49
  • 1
    Check [this](https://stackoverflow.com/questions/7795161/c-sharp-how-to-string-format-decimal-with-unlimited-decimal-places). – kovac Jul 25 '19 at 07:53

2 Answers2

3

If you have a number like this:

double number = -137855027.5123456;

You may use {0:NXX} where XX is the number of decimals you want after the decimal point. For example:

var output = string.Format("{0:N6}%", number);    // -137,855,027.512346%

You should choose the maximum number of digits you want after the decimal point. Note that if there aren't enough digits, the rest will be filled with zeros (e.g., 123.1230000). If you want to avoid that, you may do something like this:

var output = string.Format("{0:N10}", number).TrimEnd('0') + "%";

Another solution inspired by this answer would be:

var output = number.ToString("#,#.#############################") + "%";

Try them all online:

  • `Out = string.Format("{0:N10}", In).TrimEnd('0') + "%";` Works fine, any idea if this can be available in VB – monu Jul 25 '19 at 08:09
  • @monu `String.Format("{0:N10}", In).TrimEnd("0"c) + "%"`. You'll need to use a different variable name instead of `In` though. Note that both `In` and `Out` are not a good choice for variable names in either VB or C#. – 41686d6564 stands w. Palestine Jul 25 '19 at 08:12
  • Or something like: `int decimals = (decimal.GetBits(number)[3] ^int.MinValue) >> 16; string formatted = string.Format($"{{0:N{decimals}}}%", number);`. Much more readable :) – Jimi Jul 25 '19 at 08:50
  • @AhmedAbdelhameed I am able to use the C# code in Blue Prism, but when I try VB it is throwing error Expression expected from blue prism how ever online thing is working fine. Do you know which name space should be imported ? I am using Microsoft.VisualBasic but not helping. – monu Jul 26 '19 at 09:44
1

You can build your own patterns according to your input-number:

decimal myNumber = -137855027.5123456m;

// two decimals..
string pattern = "#,##0." + "".PadLeft(2, '0');
Console.WriteLine("Pattern: " + pattern);
Console.WriteLine(myNumber.ToString(pattern));
Console.WriteLine();

// 20 decimals..
pattern = "#,##0." + "".PadLeft(20, '0');
Console.WriteLine("Pattern: " + pattern);
Console.WriteLine(myNumber.ToString(pattern));
Console.WriteLine();

// Calculate how much decimals are needed..
int decimals = BitConverter.GetBytes(decimal.GetBits(myNumber)[3])[2];
pattern = "#,##0." + "".PadLeft(decimals, '0');
Console.WriteLine("Pattern: " + pattern);
Console.WriteLine(myNumber.ToString(pattern));
Console.WriteLine();

// If you only want to set the decimals this version would be easier:
Console.WriteLine(myNumber.ToString("{0:N" + decimals + "}"));
kara
  • 3,205
  • 4
  • 20
  • 34