Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
hi
i want to print the sign of a number.
for example if number is positive i want to print + (not 1)
else print -
if there is any pre defined method please help me


thank you
Posted

You could modify the format string in the following example:
double yourDouble = 2.34D;
string outputString = string.Format("{0:+0.00;-0.00;}",yourDouble);


With the format string changed to "{0:+;-;}" it just prints '+' or '-'. But Why would you separate it from the number?
 
Share this answer
 
v2
Whay dont you craete a Extension class and provide your functionalitu

like First create a custom static class with your requirements

public static class MyExtensionClass
    {
        public static string ToCustomString(this int value)
        {
            return (value > 0 ? "+" + value : value.ToString());
        }
    }


and than use it like that

int s = 1;
int s1 = -1;
Response.Write(s.ToCustomString()); //output : +1
Response.Write(s1.ToCustomString());//output : -1


By using this no need to write this conversion logic to every place.
 
Share this answer
 
Hope this[^] might help you.
 
Share this answer
 
Comments
PRASAD GDV 13-May-11 4:36am    
no i dont want to print 1 or -1

math.sign method returns only 1 or -1.
i want to print + or -
Convert Result to string and Append "+" or "-" string before printing output on screen depending on the result you are getting.
 
Share this answer
 
The answer to this depends on what you want to print onto. Do you want to print to paper, to a TextBox, or what? I don't think you are going to get away from doing something like this

C#
if (myInt < 0)
                output = "-" + myInt.ToString();
            else
                output = "+" + myInt.ToString();



Hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900