Easy question

double d1 = 8501.23
double d2 = -199

I want it to format using String.Format to ouput as
8,501.13
(199)

Thanks
LVL 13
AshokAsked:
Who is Participating?
 
HainKurtConnect With a Mentor Sr. System AnalystCommented:
try this...

function myFormatNumber(n as decimal) return string
  if n>0 then
    return ...
  else
    return "(" & -n & ")"
  end if
end function
0
 
zadeveloperCommented:
decimal.Round(100.54, 0)
0
 
AshokAuthor Commented:
double d1 = 8501
double d2 = -199

I want it to format using String.Format to ouput as
8,501
(199)

Thanks
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
käµfm³d 👽Connect With a Mentor Commented:
Per http://www.csharp-examples.net/string-format-double/
// Format
String.Format("{0:0,0.00;(0,0.00);zero}", 123.4567);

// Example using your data
String.Format("{0:0,0.00;(0,0.00);zero}", d1)
String.Format("{0:0,0.00;(0,0.00);zero}", d2)

Open in new window

0
 
käµfm³d 👽Commented:
You can remove the ".00" part of the format specifier if you do not want the decimal values to be shown.
0
 
AshokAuthor Commented:
HainKurt,

Your solution worked.
But I also found following to be working.....
if (Convert.ToDouble(value) < 0)
  return String.Format("{0:(#,###)}", Convert.ToDouble(value) * -1);
else
  return String.Format("{0:#,###}", value);

Thanks
0
 
käµfm³d 👽Commented:
Wow. Give the ppl what they ask for...    and the ppl give you an assist...  
0
 
AshokAuthor Commented:
kaufmed,

Sorry, I did not test your solution which does not require use of checking negative numbers.
As a bonus: I will post similar question with different requirements so if you answer it correctly, I will give you all points.

Thanks,
Ashok
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.