Solved

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

Posted on 2015-01-22
4
101 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 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

789 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