Value in code-behind presented differently in UI - c#

This double: 16.8999999
after theDouble = Math.Round(theDouble, 1) it shows up in the debugger as 16.9 but in the UI later on it's NOT 16.9 but 16.899999.
Why is this so?
I am developing a WP7 application and however and whenever I round a certain value, it always shows up "derounded" in the UI. I have even tried to make one last round before assigning the array to the listbox's itemssource. It's really strange and I'd appreciate any help or explanation.
I have even tried to assign a value myself just before the itemssource gets set. I assign 16.89999 and round it using the above method. In the debugger I get the expected result but then in the UI I get another result, namely, 16.899999618. Help?

Assign the value back to the variable:
mydouble = Math.Round(mydouble, 1);
Update:
Since you updated your question, you can focus on how the value is displayed instead.

Um, no: http://ideone.com/zuK9Z
I think you've made a mistake in your code. Are you doing something like this:
double theDouble = 16.8999999;
Math.Round(theDouble);
?
It should be:
double theDouble = 16.8999999;
theDouble = Math.Round(theDouble);

I have no idea but it could have something to do with how WP7 presents doubles in datacontexts. I changed the type to float and now it works perfectly. I hope someone in the same situation finds this question even though it's pretty downvoted.

Most computers use binary floating-point. Binary floating-point cannot represent 0.1 exactly (in binary, it's a recurring fraction). Thus, it also cannot represent 0.9 exactly either.

Double values are not precise by nature!
That happens because Double numbers are represented with binary digits, not decimal, so that the nearest representation of the requested number is given in the result.

Related

Can anyone Solve for x with c#? floor(x * 1.04) = 258

I'm adding the floor of 4% to an int in this example 249. It will be a different int every time though.
here is my code for that which works fine.
int qty = 249;
qty += Convert.ToInt32(Math.Floor((double)qty * 0.04));
but the problem that comes up is after this sometime later in the program and after I no longer have the original value I need to revert back to the old value using the new value. I cant just subtract 4% because although it works sometimes a lot of the time it is inaccurate. The closest I came to is this
double d = Math.Round((double)qty * 0.036);
qty = Convert.ToInt32((double)qty - d);
which is accurate most of the time with smaller numbers, but not all the time.
I did come up with an equation that should work if I could solve for x and get a whole number but so far I am stumped on how to solve for x. Here is the equation I came up with solve for x, floor(x*1.04) = 258 . Can anyone solve that and have it give me a whole number or does anyone have a better idea on how to do this without storing the old value anywhere?
I am well aware that I could store the old value but if it is easily reversible with a simple math problem then I see no reason to store it. Also this will be done on a large list of items so it wouldn't just be storing one original value it would be thousands.
This comment from #MarkDickinson was the answer I was looking for:
The mapping is injective, so it is reversible. Mathematically, you can
always recover the original quantity by using floor((qty + 1.0) /
1.04). Numerically, it's possible that there are some corner cases that might cause issues, but I didn't discover any for qty up to
10**6.

Displaying Math in a label

I'm coding a UI in C# for a particular discrete math problem (not important what) and I'd like to be able to output a proper fraction, and letters with subscripts (and superscripts) to a label. For example:
(a_1 x a_2 x ... x a_n)^6
_________________________
n
But nicely, and with actual super and subscripts. I'm familiar with LaTeX.
Any ideas?
You can also try using RichTextBoxcontrol. Check the SelectionCharOffset property to make the selection subscript and superscript. You can also use this property to position fractions.
Ok Well you can go to their and check.
Goto this URL which explain to you about fraction
Ok, So you can also try this Fraction example i think this will working example

Getting different results from 2 different computer when running the same code like Convert.ToDecimal(prm.Degeri).ToString()

When I run the code shown below, I get two different results from 2 different computers.
What might be the reason for this?
Details;
Computer-1
decimal value1=Convert.ToDecimal(prm.Degeri); //value1=6;
string svalue1=value1.ToString(); //svalue1="6";
Computer-2
decimal value1=Convert.ToDecimal(prm.Degeri); //value1=6;
string svalue1=value1.ToString(); //svalue1="6,0";
Region and language settings are all the same.
Thanks in advance.
The most likely reason for this is that prm.Degeri is "6" on Computer-1 and "6,0" on Computer-2. decimal, stores the number of significant digits (unlike double and float, which have a fixed number of significant bits), which it uses when it outputs with ToString().
It looks like it was because of serilization method. When I use "Serialization.JsonSerializeObject" to serialize the object, I have this problem. I changed it to "Serialization.SerializeObject" and problem is gone. Weird but it works. I'm sharing this solution here so hopefully this might be maybe helpful for the other people. Thank you all for spending your time.

How to convert multiple textboxes to one value

In my WPF application the user is inputting a GPS coordinates in the format of Degrees : Minutes : Seconds as decimal fraction of minutes.
So 60° 30' 45" would be entered as 60° 30.750' .
I then store it as a pure decimal number so the above example would be stored as 60.5125.
The idea was that so the users wouldn't mess up the input it would be set in 3 different textboxes. One for Degrees, other for Minutes and one for the fractionalSeconds. It's somewhat bad that one of the numbers is seperated into two textboxes but if they have to type the whole number in the are afraid of all the point or comma confusion and that they could mess up.
So one thing I thought might work was a IMultiValueConverter but it seems to only work with MultiBindings which is something I'm not doing here.
Currently my solution is to bind to different properties and do all the calculations in code behind but I'm not really happy about the fractional bit. I assume 3 fractional letters but if they enter only 7 and assume 0.700 but get 0.007 so I thought I would have to do a string format bit.
Is there a better solution out there. Can I use MultiValueConverter for something like this?
You could try using a MaskedTextBox, such as the one from the Extended WPF Toolkit.
You could use a Masked TextBox. This implementation uses MaskedTextProvider which is a .net class.

Asp.net C# math problem

I'm trying to build a "simple" web app that calculates either a male or females body fat % based on the U.S. Navy's circumference formula. I have the majority of the app completed at this point. However, I cannot figure out why the way I've setup the formula below won't work. Two of the values are underlined in red in my .cs file.
My Formula:
TBBodyFat.Text = Convert.ToString(495 / (1.0324-.19077(Math.Log(Convert.ToDouble(TBWaist.Text)-Convert.ToDouble(TBNeck.Text)))+.15456(Math.Log(Convert.ToDouble(TBHeight.Text)))));
Original Example:
%Fat=495/(1.0324-.19077(log(abdomen-neck))+.15456(log(height)))-450
Pop-Up for the two underlined values (.19077 and .15456):
struct System.Double
Represents a double-precision floating-point number.
Error:
Method name expected
TBBodyFat.Text = Convert.ToString(495 / (1.0324-.19077*
(Math.Log(Convert.ToDouble(TBWaist.Text)-Convert.ToDouble(TBNeck.Text)))+.15456*
(Math.Log(Convert.ToDouble(TBHeight.Text)))));
C# (not any programming language I've yet encountered) does not take adjacency of numbers to mean multiplication!
Well you need to use "*" for multiplication. Plus I'm not sure whether C# allows ".123" style numeric literals without leading 0.
Try:
TBBodyFat.Text =
Convert.ToString(495/
(1.0324-0.19077*(Math.Log(Convert.ToDouble(TBWaist.Text)-Convert.ToDouble(TBNeck.Text)))+0.15456*(Math.Log(Convert.ToDouble(TBHeight.Text)))));
Put * while multiplying like .8*(b-200) .If you will put directly .8(b-200) it will show error that method name expected.

Categories

Resources