I have this number: 1234.5678 (as a text)
I need this number as double, with only 2 numbers after the dot
But without Round the number
in 1234.5678 - i get 1234.57
in 12.899999 - i get 12.90
How I can do it ?
You should be able to get what you want like this:
number.ToString("#0.00")
Multiply by 100, take floor() of the number, divide by 100 again.
You didn't post the results you wanted, but I assume you want truncation, so that you'll see 1234.56, and 12.89. Try:
decimal d = 1234.89999M;
Console.WriteLine(Math.Truncate(d * 100) / 100);
This is because you can't represent these numbers exactly as doubles, so converting, rounding, and then reprinting as text results in a loss of precision.
Use 'decimal' instead.
This should do the trick.
string rawVal = "1234.5678";
System.Math.Floor((double.parse(rawVal)) * 100) / 100;
Take this floating point arithmetic!
var num = "1234.5678";
var ans = String.Empty;
if( !String.IsNullOrEmpty(num) && num.Contains('.') ) // per comment
{
ans = num.Substring(0, num.IndexOf('.') + 3);
}
This doesn't work?
double d = 1234.5678;
double rounded =Math.Round(d, 2);
Related
here is what I'm trying to do:
double result = Math.Pow((1 + 8), 60) - 1;
And the result variable is:
1.7970102999144311E+57 double
And trying to:
Math.Round(result, 5);
Returns same : 1.7970102999144311E+57 double
I'd like to round it to 1.79701 for example
Any solutions ?
You're misunderstanding what you're seeing.
1.7970102999144311E+57
Is scientific notation for
1797010299914431100000... (with 41 trailing zeros).
It is a whole number, thus rounding it to 5 decimal places will correctly return the same value.
What you want to do is format the output of the number
String.Format(CultureInfo.InvariantCulture, "{0:0.#####E+0}", result);
Which returns 1.79701E+57. Note that this is a very different number from 1.79701
The problem you're having is that Math.Round rounds things to the right of the decimal point. For example if you're dealing with currency and you perform an operation that leaves you with $1.5234524, you would use:
Math.Round(1.5234524,2);
// output 1.52
The number you're dealing with is actually scientific notation for a very large number with nothing to the right of the decimal point. This is why the result of Math.Round is the same as the input.
The earlier comments and answers are correct. But to get what you are trying to achieve you can use the following:
double result = Math.Pow((1 + 8), 60) - 1;
string s = String.Format("{0:E5}", result);
double d = Double.Parse(s);
I'm converting digital minute values to digital degrees (longitude and latitude), how would I come about retrieving all values apart from the first two? From here I can calculate values, such as 5322.72233N and 00127.5333W, to another format. I've looked at Math.Truncate (Best way to get whole number part of a Decimal number).
Let's calculate latitude 5322.72233
22.72233 / 60
+53
Final result is 53.3787055
Calculating the longitude with value 00127.5333
(Along the lines of... (calculation hasn't been checked thoroughly yet))
27.5333 / 60
+1
x -1
Final result
I'm sure it's simple.
You could just use the % operator (modulus) to remove any digits beyond 100 like this:
var input = 5322.72233m;
var output = input % 100m; // 22.72233
From that point the rest of the math should be pretty easy.
You can try to get the remainder as follows:
double a = 5322.72233;
double b = 100;
double c = a % b;
Next:
c = c / 60 + 53; // 53.3787055
I have been searching forever and I simply cannot find the answer, none of them will work properly.
I want to turn a double like 0.33333333333 into 0,33 or 0.6666666666 into 0,66
Number like 0.9999999999 should become 1 though.
I tried various methods like
value.ToString("##.##", System.Globalization.CultureInfo.InvariantCulture)
It just returns garbage or rounds the number wrongly.
Any help please?
Basically every number is divided by 9, then it needs to be displayed with 2 decimal places without any rounding.
I have found a nice function that seems to work well with numbers up to 9.999999999
Beyond that it starts to lose one decimal number. With a number like 200.33333333333
its going to just display 200 instead of 200,33. Any fix for that guys?
Here it is:
string Truncate(double value, int precision)
{
string result = value.ToString();
int dot = result.IndexOf(',');
if (dot < 0)
{
return result;
}
int newLength = dot + precision + 1;
if (newLength == dot + 1)
{
newLength--;
}
if (newLength > result.Length)
{
newLength = result.Length;
}
return result.Substring(0, newLength);
}
Have you tried
Math.Round(0.33333333333, 2);
Update*
If you don't want the decimal rounded another thing you can do is change the double to a string and then get get a substring to two decimal places and convert it back to a double.
doubleString = double.toString();
if(doubleString.IndexOf(',') > -1)
{
doubleString = doubleString.Substring(0,doubleString.IndexOf(',')+3);
}
double = Convert.ToDouble(doubleString);
You can use a if statement to check for .99 and change it to 1 for that case.
Math.Truncate(value * 100)/100
Although I make no guarantees about how the division will affect the floating point number. Decimal numbers can often not be represented exactly in floating point, because they are stored as base 2, not base 10, so if you want to guarantee reliability, use a decimal, not a double.
Math.Round((decimal)number, 2)
Casting to a decimal first will avoid the precision issues discussed on the documentation page.
Math.Floor effectively drops anything after the decimal point. If you want to save two digits, do the glitch operation - multiply then divide:
Math.Floor(100 * number) / 100
This is faster and safer than doing a culture-dependent search for a comma in a double-converted-to-string, as accepted answer suggests.
you can try one from below.there are many way for this.
1.
value=Math.Round(123.4567, 2, MidpointRounding.AwayFromZero) //"123.46"
2.
inputvalue=Math.Round(123.4567, 2) //"123.46"
3.
String.Format("{0:0.00}", 123.4567); // "123.46"
4.
string.Format("{0:F2}", 123.456789); //123.46
string.Format("{0:F3}", 123.456789); //123.457
string.Format("{0:F4}", 123.456789); //123.4568
I am calculating the average of some values. Everything works fine.
What I want to do is to round the double to the 2nd decimal place.
e.g.
I would have 0.833333333333333333 displayed as
0.83
Is there anyway to do this?
Round the double itself like:
Math.Round(0.83333, 2, MidpointRounding.AwayFromZero);
(You should define MidpointRounding.AwayAwayFromZero to get the correct results. Default this function uses bankers rounding. read more about bankers rounding: http://www.xbeat.net/vbspeed/i_BankersRounding.htm so you can see why this won't give you the right results)
Or just the display value for two decimals:
myDouble.ToString("F");
Or for any decimals determined by the number of #
myDouble.ToString("#.##")
You say displays as - so that would be:
var d = value.ToString("f2");
See Standard numeric format strings
If you actually want to adjust the value down to 2dp then you can do what #middelpat has suggested.
Use
Math.Round(decimal d,int decimals);
as
Math.Round(0.833333333,2);
This will give you the result 0.83.
double d = 0.833333333333333333;
Math.Round(d, 2).ToString();
Try
decimalVar = 0.833333333333333333;
decimalVar.ToString ("#.##");
If you want to see 0.85781.. as 0.85, the easiest way is to multiply by 100, cast to int and divide by 100.
int val = (int)(0.833333333333333333 * 100);
var result = val /100;
It should produce the result you're looking for.
how to take 6 numbers after the dot - but without round the number ?
for example:
102.123456789 => 102.123456
9.99887766 => 9.998877
in C# winforms
thak's in advance
You can use the Math.Truncate method and a 10^6 multiplier:
decimal x = 102.12345689m;
decimal m = 1000000m;
decimal y = Math.Truncate(m * x) / m;
Console.WriteLine(y); // Prints 102.123456
System.Math.Truncate (102.123456789 * factor) / factor;
In your case factor is 10^6; read more
public decimal TruncateDecimal(decimal decimalToTruncate, uint numberOfDecimalPlacse)
{
decimal multiplication_factor = (decimal)Math.Pow(10.0, numberOfDecimalPlacse);
decimal truncated_value = (long)(multiplication_factor * decimalToTruncate);
return (truncated_value / multiplication_factor);
}
I know this is ugly using strings, but thought I'd put it anyway:
double x = 9.9887766;
string[] xs = x.ToString().Split('.');
double result = double.Parse(xs[0] + "." + xs[1].Substring(0, Math.Min(xs[1].Length, 6)));
Might be a long winded way, but how about turning it into a string, locating the decimal point and then grabbing the string minus anything after the 6th decimal place. You could then turn it back into a decimal.
It's crude but how about:
decimal Number = 102.123456789;
string TruncateTarget = Number.ToString();
decimal FinalValue = Decimal.Parse(TruncateTarget.Substring(0, TruncateTarget.IndexOf('.') +6));