Number value from Science Notation to Decimal Notation - c#

I have a lot of data in Science notation when I load this data into my double variable, everything works fine (in VS I see value 0.00000000022). But when I multiply this number by 1000000 I got unrounded value (0.00021999999999999998).
I must multiply this value because I use it for selection filter. After send data from selection filter I again divide this data to my raw format 0.00021999999999999998 / 1000000 = 0.00000000022.
2.20E-10 = 0.00000000022 * 1000000 = 0.00021999999999999998
Expected value is this:
0.00021999999999999998 => 0.00022
When I use a similar number, for example, 2.70E-10 I got after multiple value 0.00027 (In this case conversion work fine).
Values are converted only for use in the selection menu so that no unnecessary zeros are shown and the label indicates which unit they represent. (For this example from Ohm to microOhm in select box)
Is there any way to correctly convert these values?
I use LINQ for convert this values in like this:
var x = y.Select(s => s.Resistance * 1000000).Distinct();

problem is floating point numbers, a good article about this is link
use decimal type instead of double for fast solving :)

Related

Round double in LINQ to Entities

The Math.Round function isn't supported by LINQ to entities (I assume that means using LINQ with the entity framework dbset) but I really need to round the double in query to filter lots of rows according to the user input.
User input is 4 digits after point and doubles in db can be any size.
Is there a way to round double in the query?
UPDATE:
Basically user enters some number in filter of the table column. Lets say it's a weight of fruits.
So for example user enters weight as 4.2152 and the column must filter all the fruits whose weight is 4.21515 or 4.215212 etc.
And in the database there are many fruits which weight is like 4.21514543543643
RESULT
So after a day long struggle I decided to use the range condition. Although it's not quite a solution. If user enters 4.2152 then range filters with condition bigger then 4.21515. But it will filter out the 4.215149 value which would otherwise be rounded to 4.2152.
The problem is solved but not exactly as needed :(
Instead of trying to round the data on your server, try instead to use some boundaries.
For your example, you basically want all the fruits which weight between 4.2152 and 4.2153 .
Now, it will depend on your specific case, (do you always want a precision of 4 decimals ? What is the exact datatype do you use, etc...), so the exact algorithm is up to you.
But it will looks like this :
double lowerBound = userInput; // 4.2152
double precision = 0.0001;
double upperBound = userInput + precision;
var query = DbSet<Fruit>.Where(f => lowerBound >= f.Weight && f.Weight < upperBound);
Also keep in mind that floating-point arithmetic can surprise you, sometimes. Depending on your usecase, this + 0.0001 might not be exactly what you want.
Update answers:
You can try to use Canonical Functions.
It's have a lot of functions help you in Math.
Maybe it can help. Document
Old answers:
Try use .AsEnumerable() before Where clause.
It's should works.

Convert double to time format

I'm new to ASP.NET MVC C# Programming
I have a class which contains an attribute called Credits of type double. This attribute should be represented as a Time Format (hh:mm) in my web application. So for example, if I were to give Credits the value of 2.5, I want it to display 2:50, 6.15 as 6:15, etc. I know TimeSpan can convert a double to the time format but it converts 2.5 to 2:30. Is there a way to do easily convert a double to this time format without changing its data type to a String? Is there some sort of helper Attribute tag or something that would make this easy?
It appears that you want to get the whole number and the fractional part separated, and that you want the fractional part as a two-digit number.
So here's how you would get the whole number part as an int.
var whole = (int)Math.Truncate(value);
And this would give you the fractional part as an int, in this case only the first two decimal places.
var twoFrac = (int)(((decimal)value % 1) * 100);
So, if value was 12.345, then twoFrac would turn out to be 34 in int.
Now you can display these whichever way you like.
And you can actually put the above inside of a method so you can get the fractional part up to however many decimal places you want, like this:
private static int GetFractionalPart(double value, int decimalPlaces)
{
var factor = (int)Math.Pow(10, decimalPlaces);
var fractional = (int)(((decimal)value % 1) * factor);
return fractional;
}
NOTE
However, as many others pointed out in comments, this is a confusing and you could even say wrong way of doing things if you're actually dealing with time values. For example, using your criteria, a value of 2.75 would convert to 2:75, but that doesn't mean anything in terms of time.
So you really ought to use TimeSpan as you mentioned in your original post, which would convert 2.5 to 2:30 etc.

Why I have different result conversion text to double

I have code here which will convert String value to Double with 2 decimal places. When the myStrValue is 820 the final result is 8.2 - which is wrong result it should be 8.20
I have code here:
string myStrValue = "820";
double myDblValue = Convert.ToDouble(myStrValue) * 0.01;
double finalValue = Math.Round(myDblValue, 2);
How to correct it?
final result is 8.2 - which is wrong result it should be 8.20
please don't mix up the number at hand and it's represenation as string when you display it. Mathematically 8.2 and 8.20 are the same numbers and are treated the same way.
On the other hand the string representation is controled by you when you decide to display it.
You can determine the format of display in different ways:
finalValue.ToString("0.00");
string rep = $"{finalValue:0.00}";
// and many more, google will find it
so there are no chance to make it 8.20? in double datatype?
Actually NO, because in datatype double it is saved entirely different, with an exponent and mantissa. If you are interested have a look at this page
But the representation of it will have no effect if you want to use the number.
Because finalValue + 0.001 will result in 8.201.

How to remove decimal part from a number in C#

I have number of type double.
double a = 12.00
I have to make it as 12 by removing .00
Please help me
Well 12 and 12.00 have exactly the same representation as double values. Are you trying to end up with a double or something else? (For example, you could cast to int, if you were convinced the value would be in the right range, and if the truncation effect is what you want.)
You might want to look at these methods too:
Math.Floor
Math.Ceiling
Math.Round (with variations for how to handle midpoints)
Math.Truncate
If you just need the integer part of the double then use explicit cast to int.
int number = (int) a;
You may use Convert.ToInt32 Method (Double), but this will round the number to the nearest integer.
value, rounded to the nearest 32-bit signed integer. If value is
halfway between two whole numbers, the even number is returned; that
is, 4.5 is converted to 4, and 5.5 is converted to 6.
Use Decimal.Truncate
It removes the fractional part from the decimal.
int i = (int)Decimal.Truncate(12.66m)
Use Math.Round
int d = (int) Math.Round(a, 0);
Reading all the comments by you, I think you are just trying to display it in a certain format rather than changing the value / casting it to int.
I think the easiest way to display 12.00 as "12" would be using string format specifiers.
double val = 12.00;
string displayed_value = val.ToString("N0"); // Output will be "12"
The best part about this solution is, that it will change 1200.00 to "1,200" (add a comma to it) which is very useful to display amount/money/price of something.
More information can be found here:
https://msdn.microsoft.com/en-us/library/kfsatb94(v=vs.110).aspx
here is a trick
a = double.Parse(a.ToString().Split(',')[0])
Because the numbers after point is only zero, the best solution is to use the Math.Round(MyNumber)
//I am doing a basic Calculation here and this works for me.
// it takes values from textboxes and performs the following as far as I understand it. as I have only been coding now for a few months.
double VC2 = Convert.ToDouble(txt_VC_M16_Tap.Text); // converts to double.
double total2 = (VC2 * 1000) / (3.14157 * 14.5); // performs the calculation.
total2 = Math.Round(total2); //Round the result to a whole number(integer)
txt_RPM2.Text = Convert.ToString(total2); // converts result to string and puts it in the textbox as required.
// Hope this helps people that are looking for simple answers.

Division and rounding of BigInteger

I'm trying to create a collection which has decimals with precision rounded to 2 decimal places. However the collection have some huge numbers and I have to use the BigInteger as the solution.
The specifics:
- Got a collection which has BigIntegers
- Got another collection of BigIntegers
- Got a third collection of Big integers
- I have to create a collection which has average value of the above 3 collections, with values rounded to 2 decimal places.
i.e if collection1 has {2,3,4} and collection2 has {4,5,5} and collection3 has {5,3,2} I should create a 4th collection which has {3.67,3.67,,3.67}
For this I'm using this code:
BigInteger divisor = new BigInteger(3.0d);
var averages = collection1.Zip(collection2, BigInteger.Add)
.Zip(collection3,
(xy, z) => BigInteger.Divide(BigInteger.Add(xy,z), divisor));
However the decimals are not appearing. I'm not sure as to whether biginteger can hold only integer values and not decimal values.
Can you please suggest a solution for this?
Note: It has to be LINQ based as the collections are pretty huge with some big values(and hence biginteger).
Well you're not getting any decimal values because BigInteger only represents integers.
Is decimal big enough to hold the number you're interested in?
If not, you might want to consider multiplying everthing by 100, and fixing the formatting side such that "1500" is displayed as "15.00" etc. You'd still need to do a bit of work to end up with ".67" instead of ".66" for a two-thirds result, as that would be the natural result of the division when it's truncated instead of rounded.
The residue after division by three is going to either be .00, .33, or .67. You should be able to determine which of those three values is appropriate. I'm not sure how you will want to have your collection store things, however, given that the numerical types that support fractions won't be able to hold your result, unless you want to define a "big integer plus small fraction" type or store your numbers as strings.

Categories