Solved

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

Posted on 2015-01-22
4
103 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 66

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 63

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 63

Expert Comment

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

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

707 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