4

I want to add more space in between

 @String.Format("AED       {0:0,0}", item.Price)

I get AED 2,999 (Only Single Space)

I want AED 2,999

Found the answer with your help, Thanks Guys

 <div style="float: right; font-weight: initial; color: Red; float: right; margin-right: 5px">
    Price -&nbsp;&nbsp;<div style="white-space: pre;float:right;">@String.Format("AED    {0:0,0}", item.Price)</div>
 </div><br />
Arun Prasad E S
  • 9,489
  • 8
  • 74
  • 87
  • 1
    You need to wrap it in an element with `white-space: pre;` (e.g.`
    @String.Format("AED {0:0,0}", item.Price)
    `)
    –  Jun 17 '16 at 06:17
  • 1
    It depends where you're doing that but take a look to: http://stackoverflow.com/questions/6286266/render-multiple-spaces-in-var and http://stackoverflow.com/questions/9492249/render-a-string-in-html-and-preserve-spaces-and-newlines – Adriano Repetti Jun 17 '16 at 06:18
  • @StephenMuecke I am using float right, but the text split up and price is showed in next line – Arun Prasad E S Jun 17 '16 at 06:37
  • 1
    In Razor View You just need to Use PRE Tag. `
    @String.Format("AED       {0:0,0}", 2999)
    `
    – RajeshKdev Jun 17 '16 at 06:38
  • Why `float:right;`? And what are you trying to do with this? It looks like `AED` is the name of a property and `Price` is its value in which case you should not be trying to format like that - use 2 separate elements. –  Jun 17 '16 at 06:40
  • @RJK when I use pre tag, i am loosing the style – Arun Prasad E S Jun 17 '16 at 06:50

2 Answers2

1

Insert unicode whitespace \x0020 or insert tab \t? Or "pre" tag.

<pre> Tag should be working:

<pre>@String.Format("AED       {0:0,0}", 2999)</pre>

enter image description here

RajeshKdev
  • 6,365
  • 6
  • 58
  • 80
1

It is already known that multiple spaces on formatted string will interpreted as single whitespace. To use multiple spaces (aka string alignment), use this way:

<pre>@String.Format("{0,-6} {1:0,0}", "AED", item.Price)</pre>

"-6" is an addition from length of indentation (length = 3) with "AED" string (length = 3) and a whitespace as your question stated above, with minus sign indicates left-aligned formatting. Change "-6" with other required length as you wish.

Edit: Preformatted text may retain formatted string with multiple whitespaces.

Edit 2: If you need to retain formatting (i.e. font style, color, etc), use either <p> or <span> tag, then add a CSS class like this way:

HTML

<span class="price">@String.Format("{0,-6} {1:0,0}", "AED", item.Price)</span>

CSS

.price {
    white-space:pre; /* or pre-wrap to enable wrapping */
}

Reference:

(1) http://www.csharp-examples.net/align-string-with-spaces/

(2) https://stackoverflow.com/a/433509 (reason how multiple spaces should use preformatted tag)

(3) https://stackoverflow.com/a/4503044 (CSS style to set preformatted whitespace)

Community
  • 1
  • 1
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61