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
Related
In my assignment, one of the instructions says: "Create a class that implements a string to hold the infix version of a math problem. A real to hold the correct answer. A real to hold the user's answer, and a boolean to represent if the problem is correct or not."
My program is to be a WPF program that would have 4 text boxes. 1st box is to show a list/stack of the equation to answer, 2nd box is to show the list/stack of equations with the right answer, 3rd box is to show the list/stack of the equations with the wrong answer, and the last box is to let the user input their answer. I thought about setting the user's input answer as an int, but seeing that I need to set it as a "real", I don't understand.
A real number is most likely what they are looking for. As a 'real' isn't a type in c#.
You could use float, double, or decimal depending upon the requirements of what you need it to do.
Real number doc: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types
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.
I have the following simple equation in my C# program to convert a number to a resulting value:
sectorSize = 1 << sectorShift;
Is there some sort of inverse operation that will allow me to go the other way as well?
sectorShift = ???
I know that you can implement a loop, but that's a little bit of an overkill. I've never had to do this before, so I have no idea and I can't find anything online about it. The equation I need only needs to produce valid results when sectorSize is a power of two; the rest of the domain can go to hell for all I care.
Here are five ways to do that in C. Translating them to correct C# is left as an exercise. Be extremely careful.
http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
Frankly, I would personally always go with the loop. It is not clear to me why you believe that simple and obviously correct code is "overkill".
Logarithms. But since you don't want to do that, use a loop and/or lookup table.
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.
What is the general opinion on the 2nd indentation method below.
// Normal indentation
a.Value = "foobar";
ab.Checked = false;
foo.Value = "foobar";
foobar.Checked = true;
// Spaces before the dot to align the properties/methods
a .Value = "foobar";
ab .Checked = false;
foo .Value = "foobar";
foobar.Checked = true;
This should probably be a wiki, but I either don't have enough privileges or don't know how to change it.
edit
I've decided to add another example to better show where such an indentation style might be useful.
fooA .PropertyA = true;
foobarB.PropertyA = true;
Changing PropertyA on all lines would be much easier with the new multi-line editing feature in VS2010.
Also having whitespace, and even line-breaks before the dot is not uncommon at all in C# (see LINQ).
Spaces before the dot? Dear God no!
I would never use either personally. I would use just traditional formatting, eg:
a.Value = "foobar";
ab.Checked = false;
foo.Value = "foobar";
foobar.Checked = true;
I understand this may not be as pleasing to the eye, but feel the others are less ideal.
Reasons being:-
Harder to maintain: Sometimes you may have smaller or larger variable names, or introduce other variables in your code which means you have to adjust formatting of all entries.
Automatic formatting might mess this up: If you use ReSharper (possibly with standard VS?) when pressing ; the formatting will adjust it back into line anyway, so you have to go out of your way to ensure it wouldn't do this.
I can't think of one right now, but I can't handle only having two points.
Edit! Thought of another point. There are more tricky keystrokes involved: For instance, for me with ReSharper to achieve the latter formatting I would type foo, enter/tab (to confirm auto-complete), tab times X amount of tabs required up to variable length (annoying), ., Value, tab to confirm auto-complete again, = then assigning data then ; and then yell at Visual Studio because all my non-standard formatting was undone as I expressed in point 2, so finally a press of CTRL+z would restore back the formatting we just had. :)
I've always used "Normal indentation." The other way seems less clear to me because I am not expecting to see indentation of that nature.
I would use spaces before the dot only if it's all the same property/method. If it's a mix of properties, like you have in your example, I'd stick with 'Normal'.
However, I think it's a moot point, because the Visual Studio autoformatter will collapse everything next time someone types a } to close any enclosing block.
Acceptable from C# language point of view.
First style is easier for me to read compared to second. I think it is because spaces before dot look very strange - in most written languages there is no space before dot.
I personally don't fight with VS IDE. Default style is fine with me.
Note that using proportional fonts totally breaks such formatting (using such fonts is extremly unusual for editing code, but ask around).
Edit: using space (+new line) for fluent interfaces looks good ( http://en.wikipedia.org/wiki/Fluent_interface#C.23).
I tried it a couple of time. Never was happy with out it looked afterwords.
I always thought aligning on the 'dot' might work in the right context:
// -----------------------
// decimal-style alignment
// -----------------------
a.Value = "foobar" ;
ab.Checked = false ;
foo.Value = "foobar" ;
foobar.Checked = true ;
But you lose the clean left margin that leads the eye down the page and maintains your sense of indent level. So you're back to keeping the dot with the identifiers it connects. However...
Aligning the equal signs is good practice as it helps order the whole set:
// -----------------------
// tabular coding
// -----------------------
a.Value = "foobar" ;
ab.Checked = false ;
foo.Value = "foobar" ;
foobar.Checked = true ;
The more order one imposes on one's code the more likely one is to notice things that are out of whack.
The parser doesn't care about tidy, readable code, but the compiler isn't the target audience for your code. Your target audience is other human beings that have to read it and comprehend it. In particular the poor schmuck — who might be you! — that has to pick up the code in a hurry and fix it 3 or 5 years from now, without no documentation to aid in understanding.
The spaces before the dots I would NEVER do, simply because as I am reading code I stop looking for a dot as soon as I see a space. The spaces after the dot, I HAVE done in the past, but hardly ever and I really prefer not to use it. The problem with using a style like that is that even if it is more readable to you, if you take such a large leap (meaning the spaces before the dot) from what might be considered a "norm" then you are going to hinder more people than help. Sticking with the standard in your team or workplace is the most important thing. Even if it is not a black and white standard, if the other 99 developers at your company us VS standard and you put all the spacing before your dots it is not going to improve anything.