Formatting floating point numbers in C#: decimal separator always visible - c#

How do I get 12. from 12 value with standard .NET string formatting? I've tested 0.### and it does not work.
double number = 12;
string.Format("{0:0.###}", number); // returns "12" not "12."
double number = 12.345;
string.Format("{0:0.###}", number); // returns "12.345"
Currently I've resolved with string manipulation but is it somewhat possible with standard string.Format()?
Thanks.

I think you can first check if the double is actually and integer, and if yes, use a simple string.Format("{0}.", number):
double number = 12;
if (number % 1 == 0)
Console.Write(string.Format("{0}.", number));
C# demo

double number = 12;
string.Format((number % 1 == 0) ? "{0}." : "{0}", number);
Gives 12.
double number = 12.345;
string.Format((number % 1 == 0) ? "{0}." : "{0}", number);
Gives 12.345

Related

Check if a string is percentage value

I'm trying to write this in C#. The requirement is very straightforward - check if a string input is a value within the range from 0 to 100.
I want to make sure the string is either an integer value in the range of 0 to 100 or
a double that's within the same range as well.
So for example, these are the accepted values:
0
50
100
0.1
50.7
100.0
I checked the double.parse method here but not sure if it's the one I'm looking for: https://learn.microsoft.com/en-us/dotnet/api/system.double.tryparse?view=net-7.0#system-double-tryparse(system-string-system-iformatprovider-system-double#)
The reason is that it can also parse string like this one: 0.64e2 (which is 64)
Is this something that can be achieved with built-in library already?
Wrote you a little snippet:
// C# function to check if string is a percentage between 0 and 100
public static bool IsPercentage(string s)
{
// regex check if s is a string with only numbers or decimal point
if (Regex.IsMatch(s, #"^\d+\.?\d*$"))
{
double d = Convert.ToDouble(s);
return d >= 0 && d <= 100;
}
return false;
}
Also returns false if the string contains % or has exponential (e).
if my understanding of the Q was correct and exponential representations like mentioned "0.64e2" is unwanted:
static bool IsPercentage(string s)
{
if (Single.TryParse(s, NumberStyles.AllowLeadingSign |
NumberStyles.AllowDecimalPoint, NumberFormatInfo.InvariantInfo, out Single n))
return n >= 0 && n <= 100;
return false;
}

Need only 2 digits after decimal point

I want my function to return value with only 2 decimal places. I have tried following code :
private static double CalculateSlabTax(int nSlabStartTaxable,
int nSlabEndTaxable,
float fSlabRate)
{
double dblSlabResult = 0;
try
{
dblSlabResult = (nSlabStartTaxable - nSlabEndTaxable) * fSlabRate;
dblSlabResult = Math.Round(dblSlabResult , 2);
return dblSlabResult;
}
catch (Exception)
{
return -1;
}
}
Expected output is : dblSlabResult = ####.## - two digits (eg. 1002.05)
Getting output as : eg. dblSlabResult = 1002.1
To represent use formatting. In your case exactly two digits after decimal point means "F2" format string
double source = 1234.56789;
// 1234.57
var result = source.ToString("F2");
Very simple. Try this
public double UptoTwoDecimalPoints(double num)
{
var totalCost = Convert.ToDouble(String.Format("{0:0.00}", num));
return totalCost;
}
The # character is an optional digit placeholder. Use 0 to enforce digits when converting to string:
string.Format("{0:#.##}", 1002.1) == "1002.1"
string.Format("{0:0.00}", 1002.1) == "1002.10"
Don't expect a specific count of digits when rounding float or double. They are just numbers, independent of their string format.
What inputs are you using? Math.Round should be working correctly, which suggests that the line above is returning a double with only 1 significant figure.

Finding number of digits of a number in C#

I'm trying to write a piece of code in C# to find the number digits of a integer number, the code works perfectly for all numbers (negative and positive) but I have problem with 10, 100,1000 and so on, it shows one less digits than the numbers' actual number of digits. like 1 for 10 and 2 for 100..
long i = 0;
double n;
Console.Write("N? ");
n = Convert.ToInt64(Console.ReadLine());
do
{
n = n / 10;
i++;
}
while(Math.Abs(n) > 1);
Console.WriteLine(i);
Your while condition is Math.Abs(n) > 1, but in the case of 10, you are only greater than 1 the first time. You could change this check to be >=1 and that should fix your problem.
do
{
n = n / 10;
i++;
}
while(Math.Abs(n) >= 1);
Use char.IsDigit:
string input = Console.ReadLine();
int numOfDigits = input.Count(char.IsDigit);
What's wrong with:
Math.Abs(n).ToString(NumberFormatInfo.InvariantInfo).Length;
Indeed, converting a number to a string is computationally expensive compared to some arithmetic, but it is hard to deal with negative nubers, overflow,...
You need to use Math.Abs to make sure the sign is not counted, and it is a safe option to use NumberFormatInfo.InvariantInfo so that for instance certain cultures that use spaces and accents, do not alter the behavior.
public static int NumDigits(int value, double #base)
{
if(#base == 1 || #base <= 0 || value == 0)
{
throw new Exception();
}
double rawlog = Math.Log(Math.Abs(value), #base);
return rawlog - (rawlog % 1);
}
This NumDigits function is designed to find the number of digits for a value in any base. It also includes error handling for invalid input. The # with the base variable is to make it a verbatim variable (because base is a keyword).
Console.ReadLine().Replace(",", String.Empty).Length;
this will count all the char in a string
int amount = 0;
string input = Console.ReadLine();
char[] chars = input.ToArray();
foreach (char c in chars)
{
amount++;
}
Console.WriteLine(amount.ToString());
Console.ReadKey();

How to format number into ten thousands [duplicate]

Is there a way using a string formatter to format Thousands, Millions, Billions to 123K, 123M, 123B without having to change code to divide value by Thousand, Million or Billion?
String.Format("{0:????}", LargeNumber)
There are different ways to achieve this, but for me the easiest and quickest is to use the "," custom specifier
double value = 1234567890;
// Displays 1,234,567,890
Console.WriteLine(value.ToString("#,#", CultureInfo.InvariantCulture));
// Displays 1,234,568K
Console.WriteLine(value.ToString("#,##0,K", CultureInfo.InvariantCulture));
// Displays 1,235M
Console.WriteLine(value.ToString("#,##0,,M", CultureInfo.InvariantCulture));
// Displays 1B
Console.WriteLine(value.ToString("#,##0,,,B", CultureInfo.InvariantCulture));
I use this mix of formats in an Extension Method (just add it to your project and enjoy) 😎
public static string ToKMB(this decimal num)
{
if (num > 999999999 || num < -999999999 )
{
return num.ToString("0,,,.###B", CultureInfo.InvariantCulture);
}
else
if (num > 999999 || num < -999999 )
{
return num.ToString("0,,.##M", CultureInfo.InvariantCulture);
}
else
if (num > 999 || num < -999)
{
return num.ToString("0,.#K", CultureInfo.InvariantCulture);
}
else
{
return num.ToString(CultureInfo.InvariantCulture);
}
}
Use:
((decimal)235).ToKMB();
// 235
((decimal)1235).ToKMB();
// 1.2K
((decimal)6271235).ToKMB();
// 6.27M
((decimal)93246571235).ToKMB();
// 93.247B
Notes:
It return more detail for bigger numbers and I like it.
It support negative numbers too. (Thanks to #Dusty for his note in comments.
I write a method for decimal numbers in this example, you can write some override methods for it to support int, long and double to use it without any casting such as:
myIntNumber.ToKMB();
myLongNumber.ToKMB();
myDoubleNumber.ToKMB();
myDecimalNumber.ToKMB();
You can implement a ICustomFormatter that divides the value by thousand, million or billion, and use it like this:
var result = string.Format(new MyCustomFormatter(), "{0:MyFormat}", number);

Representing double values in the exponential format in C#

Need to display the values in the floating point format as well as in exponential format
if the value is greater or equal to 0.01 and less than or equal to 1000
display in the expotential format else display in the floating format
For eg : 3.230000000 is displayed as 3.23
0.00001 is displayed as 1E-05
But the problem with my code if number given is 1 then the number is displayed as 1.00.
if (dValue >= 0.01|| dValue <= 1000.0)
return (string.Format("{0:0.##E+00}", dValue));
else
return (string.Format("{0:F2}", dValue));
Please let me know how to check the number does not contain decimal values
Round the number to two decimal places and to an integer, and see if the results are "close enough":
if (dValue >= 0.01 && dValue <= 1000.0)
{
if (Math.Abs(Math.Round(dValue, 2) - Math.Round(dValue, 0)) < 0.005) {
return string.Format("{0:F0}", dValue);
else
return string.Format("{0:F2}", dValue);
}
else return (string.Format("{0:0.##E+00}", dValue));
The thing to note here is that (as always with floating point numbers) the comparison between the two rounded results should not be an equality comparison.
Replace your last line
return (string.Format("{0:F2}", dValue))
to
return (string.Format("{0:0.##}", dValue))
Consider simply using G format.
Or use Math.Truncate to get integral part, that see if it ie the same as original number (need to take into account output format precision to do comparison with desider eps, probably 0.005 in your case).
Too much time. :)
You will get 2 digits maximum with values like 3.234.
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Format(0.00001));
Console.WriteLine(Format(1));
Console.WriteLine(Format(3.2));
Console.WriteLine(Format(3.22));
Console.WriteLine(Format(3.256));
Console.ReadLine();
}
static string Format(double dValue)
{
if (dValue >= 0.01 && dValue <= 1000.0)
{
int temp = (int)Math.Round(dValue * 100);
if (temp % 100 == 0)
return ((int)dValue).ToString();
else if (temp % 10 == 0)
return (string.Format("{0:F1}", dValue));
else
return (string.Format("{0:F2}", dValue));
}
else
return (string.Format("{0:0.##E+00}", dValue));
}
}
gives
1E-05
1
3,2
3,22
3,26
Probably there are cleaner solutions?!

Categories