Right justify dollars

T

tshad

I am trying to right justify my numbers using String.Format. But no matter
how I do it I end up with:

$1,000.00
$75.00

What I want to have it look like is:

$1,000.00
$ 75.00

I have tried:

String.Format("{0,10:c}"...
String.Format("{0,-10:c}"...

but both give me the same result.

Is there an easy way to do this?

Thanks,

Tom
 
O

OmegaSquared

Hello, Tom

How are you displaying the values to the user? Both TextBox and Label
provide a "TextAlign" property that you can set to right justify the text
being displayed.

Including the spaces between the "$" and the first digit would be another
problem, though. I don't know of a format specified that will do this for
you. Maybe someone else can suggest one.

Failing that however, you could do something like:

Dim decDollar1 As Decimal = 75
Dim decDollar2 As Decimal = 1000
Dim strDollar1 As String = decDollar1.ToString("F2")
Dim strDollar2 As String = decDollar2.ToString("F2")
Dim intMaxLength As Integer = Math.Max(strDollar1.Length,
strDollar2.Length)
strDollar1 = "$" & strDollar1.PadLeft(intMaxLength)
strDollar2 = "$" & strDollar2.PadLeft(intMaxLength)
TextBox1.Text = strDollar1
TextBox2.Text = strDollar2

(where the TextBoxes are right justified and have been assigned a fixed
width font (e.g. Courier).

Cheers,
Randy
 
T

tshad

Actually, I am just putting it in a string and writing it to my email.

String.Format("{0,10:c}",damt)

is supposet to right justify - which I assume would put blank fill to the
left. Not sure about the $ but the number itself doesn't right justify,
even if I change it to {0,13:c} or {0,13:######.00}

Thanks,

Tom
 
M

Martin H.

Hello Tom,
I am trying to right justify my numbers using String.Format. But no matter
how I do it I end up with:

$1,000.00
$75.00

What I want to have it look like is:

$1,000.00
$ 75.00

I have tried:

Take the numbers without the $ sign and find out which one is the
longest. For the others, add spaces until they match the length of the
longest string.

Best regards,

Martin
 
M

Mythran

tshad said:
I am trying to right justify my numbers using String.Format. But no
matter how I do it I end up with:

$1,000.00
$75.00

What I want to have it look like is:

$1,000.00
$ 75.00

I have tried:

String.Format("{0,10:c}"...
String.Format("{0,-10:c}"...

but both give me the same result.

Is there an easy way to do this?

Thanks,

Tom

String.Format("{0:###,##0.00}", ...)

If you are displaying this in an email, using html, then more than 1 space
renders as only 1 space...so '$ 75.00' becomes '$ 75.00'. You'll need to
write out non-breaking spaces if you want spaces to be kept:

Dim d As Decimal = 75d
Dim s As String = String.Format("{0:###,##0.00}", d)
s = s.Replace(" ", " ")

This is just one way, there are also other html tags you can use to preserve
white-space.

HTH,
Mythran
 
H

Hal Rosser

tshad said:
I am trying to right justify my numbers using String.Format. But no matter
how I do it I end up with:

$1,000.00
$75.00

What I want to have it look like is:

$1,000.00
$ 75.00

take a look at the PadLeft (in conjunction with the toString method) and
concatenation.
First you do the toString method to get it to a string with 2 decimals (and
no dollar sign).
Then you use padLeft to put spaces on the left of the number to make the
string a specific size overall.
Then add the $ on the left side.
You need to decide beforehand what the longest string needs to be .
HTH
Hal
 
M

Michael Williams

Then you use padLeft to put spaces on the left of the number
to make the string a specific size overall. Then add the $ on
the left side.

If the OP is using his output on something that cannot itself right justify
the string and if he is therefore forced to use padding to do it then what
you are suggesting (and what others have suggested here) will work only if
whatever is displaying the string uses or can be set to use a monospaced
font, such as Courier. For proportionally spaced fonts, which is most fonts
in Windows, you will need to add more spaces because the width of a space
character (or a comma) is less than the width of a numeric digit, and even
then only some proportionally spaced fonts will be suitable, such as Arial
where on most systems the width of the space character is equal to the width
of a comma, which in both cases is exactly half the width of a numeric
digit. All numeric digits are the same width as each other and the comma is
the same width as the space character in many, but not all, proportionally
spaced fonts, but only in some proportionally spaced fonts, such as Arial,
is the space character exactly half the width of a numeric digit, allowing
you to add two space characters for each 'missing numeric digit' and one
space character for each 'missing comma'. Other proportionally spaced fonts
would require something other than a whole number of space characters and so
with those fonts (even with this method) if the string is displayed in
something using one of those fonts then you would get only an approximation
of the output the OP is looking for.

Mike
 
H

Hal Rosser

Yep - he needs to use Courier New (or some other fixed-width font) for it to
work correctly.
I apologize for assuming that was understood.
 
T

Trevor Benedict

If you are doing this in a HTML email, you could think of nesting the $ sign
inside a <TD> tag and right justify it and left justify the numbers.
Whatever to the left or right of this number must be inside another TD tag
like below

<table>
<tr>
<td>my other text</td><td align="right">$</td><td
align="left">9999</td>
<td>my other text</td><td align="right">$</td><td
align="left">999</td>
<td>my other text</td><td align="right">$</td><td
align="left">99</td>
<td>my other text</td><td align="right">$</td><td
align="left">9</td>
</tr>
</table>

Regards,

Trevor Benedict
 
T

tshad

Martin H. said:
Hello Tom,


Take the numbers without the $ sign and find out which one is the longest.
For the others, add spaces until they match the length of the longest
string.

I'll look at that but why doesn't the String.Format work?
 
M

Michael Williams

I'll look at that but why doesn't the String.Format work?

Probably because you are either formatting it incorrectly or because you are
using a proportionally spaced font. What font are you using to display the
output?

Mike
 
M

Martin H.

Hello tshad,
I'll look at that but why doesn't the String.Format work?

Because String.Format does not provide any padding as it doesn't know
which font you use. What it does is something like this:

Dim i As Integer = 12
MsgBox(i.ToString("#.00")) 'Results in 12.00

Dim s As Single = 1.234567890
MsgBox(s.ToString("0.000"))'Results in 1.235

Best Regards,

Martin
 
J

James Hahn

What you are trying to achieve is not right justification. A string that is
right justified is formatted and placed in the right-hand portion of the
allocated space. The formatting applies before the alignment, so a right
justified currency will be something like
$75.00
depending on absolute value of the alignment component.

What you are trying to do could be called full justification - the output
string is formatted to occupy the whole of the allocated space by dividing
the string at a defined point - just after the dollar sign. The
string.format method does not provide that facility. You can achieve what
you are trying to do using a format such as

String.Format("${0,8:F2}", v)

However, you will need to deal with negative values for yourself. For
instance:

IIf(v >= 0, String.Format(" ${0,8:F2}", v), String.Format("(${0,8:F2})",
Math.Abs(v)))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top