Decimal places float [duplicate] - c#

This question already has answers here:
Limiting double to 3 decimal places
(8 answers)
Closed 8 years ago.
I'm have a hard time trying to format a float with 4 decimal places, for example, I have the number 2.999995, I wanna get only 4 decimal places from this number, 2.9999, when I use .toString("#.####") or .toString("0.0000") it return 2.3000, I don't wanna this, I wanna 2.9999, someone can help me?
Thanks

Try This:
float floatVal = 2.999995f;
string str=floatVal.ToString();
if(str.Split('.')[1].Length > 3)
floatVal=Convert.ToSingle(str.Substring(0,str.IndexOf('.')+5));
Console.WriteLine(floatVal);
Output:
2.9999
Demo here

Related

Is there a way to display 2 decimal places instead of automatically rounding? [duplicate]

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 3 years ago.
I'm making a simple calculator in c# and and everything works well until I try dividing numbers that should give decimal places. How to display 2 decimal places in these cases?
I've tried putting #.## after .ToString.
{label1.Text = (divide / Convert.ToInt64(label1.Text)).ToString("#.##");}
I expect the output of 5/4 to be 1.25, but it is 1.
convert the number to float instead of int,
{label1.Text = ((float)divide / float.Parse(label1.Text)).ToString("n2");}

How to round a decimal number? [duplicate]

This question already has answers here:
Rounding a variable to two decimal places C# [duplicate]
(8 answers)
How do you round a number to two decimal places in C#?
(15 answers)
Closed 5 years ago.
I have a decimal number decimal n = 0.111111111m; and I want to change it to 0.112. How could I do this ?
You can use:
Math.Round(n, 3);
To always round up, you can use:
Math.Ceiling(n * 1000) / 1000;
According to this link
https://msdn.microsoft.com/en-us/library/6be1edhb(v=vs.110).aspx
You'll need to do something like Decimal.Round(n, 3);
However your initial approximation is not accurate

taking just two digits of a Number of type double without rounding it [duplicate]

This question already has answers here:
Truncate Two decimal places without rounding
(24 answers)
Closed 8 years ago.
I have this Number
double Nb=4.9584763251
how can I make get only two digits after the comma without rounding the number :
Nb=`4.95` and not `4.96`
using the Math.Round(Nb,2) will return 4.96
is there any way to fix this ?
You may use Math.Truncate(Nb * 100) / 100m
double x = ((int)(Nb*100))/100.0

Trimming a float [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Leave only two decimal places after the dot
Formatting a float to 2 decimal places
If I have a float that consists of something like 153.2154879, is there any way to convert it to string but only show 4 decimal places? I know I can format it using "000.000", but the front number doesnt always have to be 3 digits. So is there a way to show all the front numbers (153), but only the first 4 characters after the point in a string?
Something like this should do:
your_number.ToString("0.####");
This will show a max of 4 decimal places.
I usually use a format string like "#0.0000".
You can use the C# function Math.Round function.
float a= 153.213456;
Math.Round(a,3);
this would round up the number to 153.213
then get convert it to string.

c# - Show a decimal to 6 decimal places [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Double.ToString with N Number of Decimal Places
I want to show a decimal to 6 decimal places, even if it contains 6 x 0's
For example:
3.000000
5.100000
3.456789
and so forth, is this possible?
Use N6 as the numeric format string.
myDecimal.ToString("N6");
or
string.Format("{0:N6}", myDecimal);
Decimal d = 20;
d.ToString("0.000000");

Categories