উদাহরণ সরূপ নিচের sample input এবং expected output এর কথা চিন্তা করুন-
using System;
namespace CSharpPrograms
{
class Program
{
static void Main(string[] args)
{
//creating a sample array for test data
decimal[] decimalNumbers = { 1, 1.0M, 1.1M, 1.12M, 1.123M, 1.1100M,
1.010M, 1.001100M };
//loop thru the each decimal number
foreach (var item in decimalNumbers)
{
Console.WriteLine("Orginal Number={0}, Total decimal Places=
{1}", item, GetDecimalPartCount(item));
}
Console.ReadKey();
}
//function for returning total decimal places
public static int GetDecimalPartCount(decimal n)
{
int decimaldigitcount = 0;
//getting only decimal part
decimal decimaldigit = n % 1;
if (decimaldigit != 0)
{
//Get the index of dot from the decimal part
int indexOfDot = decimaldigit.ToString().IndexOf(".");
// Use the 0.######## format string to rip off trailing zeros, and get the count
decimaldigitcount =
decimaldigit.ToString("0.#########"). Substring(indexOfDot).Length - 1;
}
return decimaldigitcount;
}
}
}
Input | Output |
1 | 0 |
1.0 | 0 |
1.1 | 1 |
1.12 | 2 |
1.123 | 3 |
1.1100 | 2 |
1.010 | 2 |
1.001100 | 4 |
using System;
namespace CSharpPrograms
{
class Program
{
static void Main(string[] args)
{
//creating a sample array for test data
decimal[] decimalNumbers = { 1, 1.0M, 1.1M, 1.12M, 1.123M, 1.1100M,
1.010M, 1.001100M };
//loop thru the each decimal number
foreach (var item in decimalNumbers)
{
Console.WriteLine("Orginal Number={0}, Total decimal Places=
{1}", item, GetDecimalPartCount(item));
}
Console.ReadKey();
}
//function for returning total decimal places
public static int GetDecimalPartCount(decimal n)
{
int decimaldigitcount = 0;
//getting only decimal part
decimal decimaldigit = n % 1;
if (decimaldigit != 0)
{
//Get the index of dot from the decimal part
int indexOfDot = decimaldigit.ToString().IndexOf(".");
// Use the 0.######## format string to rip off trailing zeros, and get the count
decimaldigitcount =
decimaldigit.ToString("0.#########"). Substring(indexOfDot).Length - 1;
}
return decimaldigitcount;
}
}
}
No comments:
Post a Comment