Solved

# Easy question

Posted on 2010-01-07
Medium Priority
254 Views
double d1 = 8501.23
double d2 = -199

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

Thanks
0
Question by:Ashok

LVL 13

Expert Comment

ID: 26202285
decimal.Round(100.54, 0)
0

LVL 61

Accepted Solution

HainKurt earned 600 total points
ID: 26202323
try this...

function myFormatNumber(n as decimal) return string
if n>0 then
return ...
else
return "(" & -n & ")"
end if
end function
0

LVL 13

Author Comment

ID: 26202388
double d1 = 8501
double d2 = -199

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

Thanks
0

LVL 75

Assisted Solution

käµfm³d   👽 earned 400 total points
ID: 26202512
Per http://www.csharp-examples.net/string-format-double/
``````// Format
String.Format("{0:0,0.00;(0,0.00);zero}", 123.4567);

String.Format("{0:0,0.00;(0,0.00);zero}", d1)
String.Format("{0:0,0.00;(0,0.00);zero}", d2)
``````
0

LVL 75

Expert Comment

ID: 26202519
You can remove the ".00" part of the format specifier if you do not want the decimal values to be shown.
0

LVL 13

Author Comment

ID: 26202602
HainKurt,

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

LVL 75

Expert Comment

ID: 26202649
Wow. Give the ppl what they ask for...    and the ppl give you an assist...
0

LVL 13

Author Comment

ID: 26205814
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
###### Suggested Courses
Course of the Month14 days, 21 hours left to enroll