473,464 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help with conversion

Hi,
How can I convert positive integers to negative integers. Eg 1 to -1.

Thanks
Nov 22 '05 #1
5 932
Chris wrote:
Hi,
How can I convert positive integers to negative integers. Eg 1 to -1.


Not to be a smart aleck, but I find multiplying by -1 to be useful. You
can guarantee a negative result by first taking the absolute value, so

Result = Math.Abs(x) * -1

will always return a negative number.
--
Gregory Gadow
Nov 22 '05 #2
Based on performance. Do you think this is fast

TextBox2.Text = (Abs(Convert.ToDecimal(TextBox1.Text)) * -1).ToString()

Thanks

"Gregory Gadow" wrote:
Chris wrote:
Hi,
How can I convert positive integers to negative integers. Eg 1 to -1.


Not to be a smart aleck, but I find multiplying by -1 to be useful. You
can guarantee a negative result by first taking the absolute value, so

Result = Math.Abs(x) * -1

will always return a negative number.
--
Gregory Gadow

Nov 22 '05 #3
Easiest way

myNumber = myNumber * (-1);

or silly way

myNumber = myNumber - (myNumber * 2);
short form is

myNumber -= myNumber * 2;
Mihir Solanki
http://www.mihirsolanki.com
Hi,
How can I convert positive integers to negative integers. Eg 1 to -1.
Thanks

Nov 22 '05 #4
Easiest way

myNumber = myNumber * (-1);
Why is that ieasier than

myNumber = - myNumber;

or silly way

myNumber = myNumber - (myNumber * 2);


Also increased risk of overflowing.
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 22 '05 #5
Chris wrote:
Based on performance. Do you think this is fast

TextBox2.Text = (Abs(Convert.ToDecimal(TextBox1.Text)) * -1).ToString()

Thanks
/shrug

Any time you want to do math with text, the text must first be converted to a
numeric form. If you want to represent the numeric result as text, you need
to convert it back into a string. There is no way around this.

If you have code elsewhere to guarantee that the value in TextBox1 will
always be a positive integer, you can dispense with the call to Abs(). I
*think* that explicitly calling the ToType functions improves performance a
bit over having the conversions done implicitly. So yes, your example is
about the best you will be able to do.
--
Gregory Gadow
"Gregory Gadow" wrote:
Chris wrote:
Hi,
How can I convert positive integers to negative integers. Eg 1 to -1.


Not to be a smart aleck, but I find multiplying by -1 to be useful. You
can guarantee a negative result by first taking the absolute value, so

Result = Math.Abs(x) * -1

will always return a negative number.


Nov 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thames | last post by:
Hi, I have to repost my question for help. Yesterday I set up two DB UDB ESE V8.2 server on linux and windows platform, respectively. Both servers have been connected without any problems...
11
by: Marcus Jacobs | last post by:
Dear Group I have written a file conversion program that uses strtof to convert text strings to floats. It works as I intended except for my error messages. It is my understanding that strtof...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
4
by: Gregory Edigaroff | last post by:
Hello, Everybody! Ok, I urgently need the solution for a following task in C++. This is the task from programmers contest, so I believe somebody have a solution for it. I need either a full...
0
by: k1ckthem1dget | last post by:
I need to display the unsorted list of names and display the sorted list of names. My program is getting a bunch of errors though, and i dont know why. I am getting the following errors. 28:...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.