Solved

C# Padding ,with negative sign infront of padding if number is less than 0

Posted on 2015-01-22
4
98 Views
Last Modified: 2015-01-22
I would like assistance on how I can write below like  on step a.  the code provided below is writting like on step b.  I would like to put the nagtive infront of padding  instead of it padding first with 0 and then put negative sign with the amount.

-157.07  as Amount --  My data coming from the reader looks like this .

a.               -000000157.07
b .               000000-157.07



 double amount = 0;
            amount = amount + Double.Parse(reader["Amount"].ToString());
            string stramount = amount.ToString("F", CultureInfo.InvariantCulture);
writeline( stramount.PadLeft(13, '0'))

Please help.  data is coming from sql server
0
Comment
Question by:valentinemhlanga
  • 2
4 Comments
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 200 total points
ID: 40564642
Don't know about C# specific, but you're missing the absolute value ABS() function to write the number.

The T-SQL would be..
Declare @amt money = -157.07

SELECT 
   CASE WHEN @amt < 0 THEN '-' ELSE '0' END + 
   RIGHT('000000000' + CAST(ABS(@amt) as varchar(12)), 12) 

Open in new window

0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 300 total points
ID: 40564700
Hi valentinemhlanga;

The following line of code should give you what you need.

Console.WriteLine("{0:000000000.00}", amount);
0
 

Author Closing Comment

by:valentinemhlanga
ID: 40564825
Thank you Fernando Soto and : Jim Horn for your quick responses , I ended up using Fernando 's  answer as it was just making few changes to my code
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40564836
Not a problem valentinemhlanga, glad to help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now