-1

Code first.

taxRateDecimal = 0.0765;
if (taxRateDecimal.ToString().Split('.').Length == 2)
{
    outPut =  taxRateDecimal.ToString().Split('.')[1].Substring(0, 4);
    ReportParameters.Add("TaxRateAsDecimal", "." + outPut); 
}

As you can see in the substring I have to hard code my length of 4. Desired results .0765 as a string.

Here are some scenarios I have gone through.

I have a percent of 7.65. I want to convert this to decimal, when doing so I do 7.65 / 100. this gives me 0.0765, but I want just .0765. If I take 0.0765 and use a split on "." and use length ect.. I actually get for the array [1] a length of 6 and a value of 076500. Again I only want .0765 or just 0765 and I can add the dot.

Any other ideas that I have not tried? This will eventually need to be a string since I am passing it in as a prama into SSRS.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
kss113
  • 7
  • 3
  • 1
    Are you just trying to format a decimal into a string? – ryanyuyu Mar 25 '15 at 19:46
  • http://stackoverflow.com/questions/164926/c-sharp-how-do-i-round-a-decimal-value-to-2-decimal-places-for-output-on-a-pa its not exactly what you want but it should be of help. – marsh Mar 25 '15 at 19:47

2 Answers2

2

You should be able to use a format to create the string:

var taxRateDecimal = 0.0765D;
var reportParameter = taxRateDecimal.ToString(".0000", CultureInfo.InvariantCulture);

The resulting reportParameter is .0765.

Other results for that format are:

0D       ->  .0000
1D       -> 1.0000
1.23456D -> 1.2346

You have to use CultureInfo.InvariantCulture to be sure that the decimal separator is a dot. Otherwise the current culture (which is the default) might specify a comma as the decimal separator.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
1

string.Format will handle this easily.

double taxRateDecimal = 1.0765;
string formattedResult = string.Format("{0:.0000}", taxRateDecimal % 1);

Console.WriteLine(formattedResult);  // .0765
recursive
  • 83,943
  • 34
  • 151
  • 241