[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 108
  • Last Modified:

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

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
valentinemhlanga
Asked:
valentinemhlanga
  • 2
2 Solutions
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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
 
Fernando SotoCommented:
Hi valentinemhlanga;

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

Console.WriteLine("{0:000000000.00}", amount);
0
 
valentinemhlangaAuthor Commented:
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
 
Fernando SotoCommented:
Not a problem valentinemhlanga, glad to help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now