string.format() with at least one decimal place [duplicate] - c#

This question already has answers here:
Display Float as String with at Least 1 Decimal Place
(4 answers)
Closed 7 years ago.
From the following two - double precision - numbers:
123456
0.0003232
I need to get (at least one decimal place) ...
123456.0 (one decimal place added)
0.0003232 (same as above)
... and never scientific notation, like E+000. The more close result from standard string.Format() is string.Format("{0:F1}", myDoubleVal) but in the second case the decimals are lost.
What else can I try?
Thanks.

Try the below
string.Format("{0:0.0###########}",myval);
Thanks

Related

Remove decimals without rounding [duplicate]

This question already has answers here:
How to remove decimal part from a number in C#
(8 answers)
Closed 2 years ago.
I have some variable length decimal numbers for ex:
1.123,
1.1234,
12.12345,
I need only the first 4 decimals. I can't use Math.Round() because I do not want rounded numbers, I just want to trim it and keep only first 4, like 1.1234, or 22.1234. Is there a way to accomplish this?
Thank you.
Multiply by 10000, store as integer to get rid off any remaining decimals. Divide by 10000.
var result = number.ToString("n4");
check Standard numeric format strings

C# Remove certain amount of numbers end of line [duplicate]

This question already has answers here:
How to limit a decimal number? [duplicate]
(6 answers)
Closed 6 years ago.
So,
I got example number: 0.00857382942837523 which length is 18, but I would like to remove from the end example 9 numbers. Thing is, that number is different every time, so I can't "predict" numbers what are going to be at the end.
//My question was marked to duplicate, but it can't be solved like that. Mine has 0.00, if I would do math.round it would give up 0...
You need Math.Truncate
decimal d= 0.00857382942837523m;
d= Math.Truncate(d * 1000000000m) / 1000000000m;
Console.WriteLine(d); // 0.008573829

How to convert string to decimal with specific number of digits [duplicate]

This question already has answers here:
Formatting a double to two decimal places
(8 answers)
Closed 6 years ago.
I have a small question regarding Math.Round function.
I need the string "12.123456" to be rounded at 4 decimals. I used:
Math.Round(Convert.ToDouble(pData), 4).ToString()
where pData is defined as string, but the values are decimal with 7 decimals.
My problem is that I expected to get every time the exact 4 decimals, but for some values it gives me only 2 (eg. 12.12 instead of 12.1200).
How can I change in order to always get the needed 4 decimals?
Regards,
You should use format strings instead:
pDate.ToString("0.0000")
or
pDate.ToString("n4")

How to determine the # of decimals in a double? [duplicate]

This question already has answers here:
Find number of decimal places in decimal value regardless of culture
(20 answers)
Closed 8 years ago.
If I have a number, how can I determine the number of decimals?
e.g. for 0.0001 I want to get the result 4
The duplicate suggested above is less suitable than this one because
they are taking about culture-independent code but this question is
just about decimal oriented code (i.e. after the decimal). So no need
to introduce any more overhead:
Finding the number of places after the decimal point of a Double
but they both are good threads.
You can't really. A double is a floating point precision data type, so it's never precise.
You could hack something around, using ToString:
double d = 0.994562d;
int numberOfDecimals = d.ToString(CultureInfo.InvariantCulture).Length
- d.ToString(CultureInfo.InvariantCulture).IndexOf('.')
- 1
;

Format to two decimal places [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
c# - How do I round a decimal value to 2 decimal places (for output on a page)
I have a number
long n = 32432432423;
I want to divide this by 1450 and print on a console with 2 decimal places (rounded)
How can I do it?
COnsole.WriteLine(????);
Console.WriteLine("{0:N2}", ((double)n) / 1450);
Console.WriteLine("{0:0.00}", 32432432423 / 1450.0);
make use of Math.Round
Console.WriteLine( Math.Round(Convert.ToDecimal(32432432423 / 1450.0), 2));
Result: 22367194.77

Categories

Resources