Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Add multiple space in C# string format function Razor? [duplicate]

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 />
like image 937
Arun Prasad E S Avatar asked Oct 28 '25 10:10

Arun Prasad E S


2 Answers

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

like image 102
Alexey Kleandrov Avatar answered Oct 30 '25 00:10

Alexey Kleandrov


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)

like image 39
Tetsuya Yamamoto Avatar answered Oct 30 '25 01:10

Tetsuya Yamamoto