Solved

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

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

861 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

23 Experts available now in Live!

Get 1:1 Help Now