Remove Decimal points from Price Shown on Home Page

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
Dear All,

i want to show price like 50 (PKR)  but it is showing like 50.00 (PKR)  i want to remove decimal points

please guide me if any one knows how to do this


regerds
Javaid
6 years ago
Open this view with notepad:
/Views/Product/_ProductPrice.cshtml

Find this line of code at around line 50:

@Html.Raw(Model.Price)


You need to do some formatting on that value.

I think something like this will work:

string pricewithnodecimal=String.Format("{0:C0}", Model.Price)


But keep in mind that it will affect everywhere on the site where the prices are displayed UNLESS you write it into a conditional statement (if/then/else/if) based on the currently viewed page, like:

string currentpage=HttpContext.Current.Request.ServerVariables["PATH_INFO"];
if (currentpage == "/") {

@*  do your string formatting here *@

string pricewithnodecimal=String.Format("{0:C0}", Model.Price)
@pricewithnodecimal

}
else
{

@Html.Raw(Model.Price)

}
6 years ago
Re: String.Format("{0:C0}", Model.Price)
I don't think that will work in the View, because Model.Price is a string.  The decimal price has had the formatting applied.

In the Admin > Configuration > Currencies, you can Edit a currency and set its "Custom Formatting" field to "{0:C0}", but that will apply to all* pages that display the price and not just the 'home page' as per the title of this topic.


(* well, not really "all".  There are also string resources, like this one used when there are 'variants':
products.pricerangefrom       From {0}.

You can search 'String resources' for others:
Resource name:  price
Value:          {
2 years ago
I know it's late, but for share my experience
in my case i only  wanted to remove leading zeros from price at admin panel
and the price show correctly at the front
so at Decimal.cshtml file in editor templates
we have below codes :
@model Decimal
<input asp-for="@Model" asp-format="{0:F4}" />
@{
    var postfix = "";
    if (ViewData.ContainsKey("postfix") && ViewData["postfix"] != null)
    {
        postfix = ViewData["postfix"].ToString();
    }
}
<script>
    $(document).ready(function () {
    $("#@Html.IdForModel()").kendoNumericTextBox({
        min: @Html.Raw(decimal.MinValue),
        max: @Html.Raw(decimal.MaxValue),
        decimals: 4,
        restrictDecimals: true,
        format: "#.0000 @postfix"
        });
    });
</script>


I changed to this then the zeros are removed

@model Decimal
<input asp-for="@Model" asp-format="{0}" />
@{
    var postfix = "";
    if (ViewData.ContainsKey("postfix") && ViewData["postfix"] != null)
    {
        postfix = ViewData["postfix"].ToString();
    }
}
<script>
    $(document).ready(function () {
    $("#@Html.IdForModel()").kendoNumericTextBox({
        min: @Html.Raw(decimal.MinValue),
        max: @Html.Raw(decimal.MaxValue),
        decimals: 4,
        restrictDecimals: false,
        format: "# @postfix"
        });
    });
</script>
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.