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

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

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

Math Round in VS C# [duplicate]

This question already has answers here:
How do I display a decimal value to 2 decimal places?
(19 answers)
Closed 5 years ago.
so I have the following problem I'm trying to output 2 numbers after the decimal comma but it doesn't output it with 2 numbers only when I have zero or more than one zero at the end of the number.
Console.WriteLine(Math.Round(s * 0.07, 2));
here's how I did it.
You might want:
Console.WriteLine($"{s:N2}");

Get a pair of integers from an integer [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get the seventh digit from an integer
I have an integer and I want a pair number from it.
var myDigit = 2345346792;
I need the 5th and 6th number out from myDigit namely 34.
What is the mathematical way of getting them without any use of substring?
Additional question from my previous thread:
Get the seventh digit from an integer
Repeat your previous solution, using 100 instead of 10 for the modulus and adjusting the division.

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

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

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