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 />
6
  • 1
    You need to wrap it in an element with white-space: pre; (e.g.<div style="white-space: pre">@String.Format("AED {0:0,0}", item.Price)</div>) Commented Jun 17, 2016 at 6:17
  • 1
    It depends where you're doing that but take a look to: stackoverflow.com/questions/6286266/… and stackoverflow.com/questions/9492249/… Commented Jun 17, 2016 at 6:18
  • @StephenMuecke I am using float right, but the text split up and price is showed in next line Commented Jun 17, 2016 at 6:37
  • 1
    In Razor View You just need to Use PRE Tag. <pre>@String.Format("AED {0:0,0}", 2999)</pre> Commented Jun 17, 2016 at 6: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. Commented Jun 17, 2016 at 6:40

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

@String.Format("AED \t{0:0,0}", item.OfferPrice) did not work
@String.Format("AED \x0020\x0020{0:0,0}", item.OfferPrice) did not work
It worked but i lost the styles, only color was there
you gotta be adding more css code to make pre tag more stylish :)
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)

3 Comments

And as soon as its rendered in the browser it will be collapsed again :)
Use <pre> tag around the output is possible to retain returned string.
@TetsuyaYamamoto a little adjustment to the pre tag functionality made my day

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.