Split Double Values? - c#

i have two values written like this " +5.000" (first space then plus and the double value, double value is height in meters)
the first one is on textBox and second one is i'm receiving via ref key.
simply enough i want to get the result firstvalue - secondvalue = result
for example ( +5.000 - +2.800 = 2.200)
result only in digits without plus.
i have asked this question also on c# forum
https://social.msdn.microsoft.com/Forums/en-US/a024e097-8013-4771-bbf6-99c7fd4cf457/double-split-
Thanks and Best Regards

OK, I think you're trying to solve a few problems here in one question. Let's break it down.
You need to get a value from a text box. I'll assume its called txtBox in the absense of any code, so you need to write:
double a = Double.parse(txtBox.Text);
You then need to perform your calculation. This needs to be written the other way around, for example:
result = a - b;
With limited source code it's difficult to answer properly.

Related

Spliting a number into equal parts and Add it together to get the same number

I need to implement a logic in c# where as I need to split and Add it to together to get the same value.
For example:
1.0/6.0442137639369475 = 0.16544749061764519
and when I add
0.16544749061764519 + 0.16544749061764519
to make it 1.0 is not working at all it comes as
0.99268494370587
not 1.0 exactly.
I am not sure what I am missing...?
Try to use more precise dataformats. Try to use double. If this is not precise enough, what I doubt, you can still search for a more precise implementation.
if I guess your question correct, you want to do the following math:
c=a/b
d=c+c+c+... n-times until a==d.
this will only work if you use integer numbers for a and b, with your real number 6.0442137639369475 as b it cannot (math law afaik).
And by the way:
your division result 0.16544749061764519 is rounded! The correct result is 0.16544749061764518326.
So you maybe need another data type as already pointed out.

How can I increment and decrement a unicode character by 1?

I am writing a small program with a label and 2 buttons. The label is initialized with a unicode character, for example "\u221A" (√).
I would like 1 button to increment the unicode value of the label, say to "\u221B", then to "\u221C", etc, and another to decrement it, say to "\u2219", then "\u2218", etc.
I have no idea where to begin, and have googled for quite a while. I've tried doing stuff myself, but none of it compiles.
char is a numeric type. You can add and subtract numbers from it, then cast it back to char and create a string from it.
Without code I can't give a specific answer. However perhaps this is what you're thinking of:
char ch = label.Text[0]; // assumes label is not empty; get first character of string
++ch; // increment; use -- to decrement instead
label.Text = ch.ToString(); // back to string
Add your own error handing and range checking. Also keep in mind that many code points aren't defined and many others won't display in certain fonts.

Parsing in CSharp - how to understand it

It's just few days ago that I jumped into learning C# and I already have one problem with understanding basics.. Maybe it's just the language barrier (I'm not English native speaker). Please, could you explain me how to understand parsing? For example: while creating a very simple calculator I wanted to read the first input number (which is a variable a). I use this code:
float a = float.Parse(Console.ReadLine());
and the same with b for the other number:
float b = float.Parse(Console.ReadLine());
I learnt that the float is a data type for decimals numbers so what exactly does this particular Parse() stands for?
Obviously, I tried to run the application without parsing and it wouldn't work because it reads it as string, but why? Thank you..
Console.ReadLine() returns a string, which represents a piece of text. So, from the computer's point of view, what you have after calling Console.ReadLine() is a piece of text. It may or may not contain the text "6.0", but from the computer's point of view, it is just a piece of text. As such, you cannot use it to add, subtract etc.
Using the float.Parse(...) method, you tell the computer: "This piece of text actually represents a floating point number, could you please read the text and give me back a number, so that I can start doing math with it?".
The method you are using, float.Parse() is just one of many such methods that take a String input value, and attempt to convert it into the target type, here a float.
There is a safer alternative, however, and it is TryParse():
float a;
if (float.TryParse(Console.ReadLine(), out a))
{
//do something with your new float 'a'
}
In either case, your are asking the framework to inspect the value you provide, and attempt to make a conversion into the requested type. This topic can be quite deep, so you'll want to consult MSDN for the specifics.
Console.ReadLine reads text that the user inputs and returns it to the program so that you may do with it what you want. Therefore, the ReadLine method returns a string.
If you want to work with a decimal (check the decimal class instead of float), you need to convert the string, which is a character sequence, to a number of your desired type, that's where float.Parse comes in:
float.Parse accepts a string and if possible, returns a float value.
Almost every type contains the Parse method which is used to transform a string into the calling one.

I need to get the data from double values without having decimal places

I have a double variable as shown Below
double var1 = 0;
And
I Have a Value = 123.25.
For Example var1 += Math.Round(123.25), I am getting 123.0
So I need to get the Data Like as shown Below?
Output should be = 123
The problem isn't in the data - it's in the string conversion. There's no difference for a double between 123.0 and 123... they're exactly the same values, with the same bits.
So instead of focusing on the double value itself, you should look at how you're using it - which you haven't told us about. For example, you might just want to change the format pattern you're using.
How about using:
(int)var1
Wherever you need to show its value?
int someOtherInt = Convert.ToInt32(var1);
Based on your question. If you only want the integer value why not have the Math.Round() result be assigned to a type int? That would get you what you want.
int x = (int)Math.Round(123.25);
I cannot understand the reason you don't like the .0 at the end? If you print the variable holding 123.0 it shows up as 123. I can only see 123.0 when using debugger.
How are you using the value? If you could tell us then we could find a better solution if none of the ones provided are of use to you.
I would strongly recommend that you read this: http://www.whathaveyoutried.com
If you follow the suggestions in that article, you will be able to get help in solving your problem in no time.
Your getting confused with the types. You don't want to use double in this situation by the sounds of it. Instead, use int.
int var1 = 0;
int var2 = (int)123.25;
int output = var1 + var2;
output in this case is 123. Cast to int removes the need for Math.Round.

Find the x, y point of a string within a text box

Is there a way to return a point for a string within a text box? I found a COM function GetTextExtentPoint that will return the length of a string, but I want to know the point where the string starts.
You're looking for the GetPositionFromCharIndex method.
First, figure out the index of the first character of the string.
int index = textBox1.Text.IndexOf(someString);
Then use GetPositionFromCharIndex.
Point stringPos = textBox1.GetPositionFromCharIndex(index);
(Code not tested, but something like this should work. Of course you will have to deal with the possibility of duplicate occurrences of your string in the textbox.)
what comes to mi mind is to take a snapshot of both the form and text then do some fancy image comparing to find the starting point.. but for this you need to write/download a library that has theese comparing methods... thus becoming very complicated...
why do you need to do this?

Categories