1

I want to display my latitude and longitude to 8 decimal places. However, I am only displaying it to 2 decimal places by default right now. How should I change my Model?

Model:

    public class LocationModel
    {
        [Display(Name = "Latitude")]
        public decimal Latitude { get; set; }

        [Display(Name = "Longitude")]
        public decimal Longitude { get; set; }
    }

1 Answer 1

2

Two options:

  1. DataFormatString
public class LocationModel
{
    [Display(Name = "Latitude")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:G8}")]
    public decimal Latitude { get; set; }

    [Display(Name = "Longitude")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:G8}")]
    public decimal Longitude { get; set; }
}
  1. Math
public class LocationModel
{
    private decimal _latitude;
    private decimal _longitude;

    [Display(Name = "Latitude")]
    public decimal Latitude
    {
        get
        {
            return Math.Round(_latitude, 8);
        }
        set
        {
            this._latitude = value;
        }
    }

    [Display(Name = "Longitude")]
    public decimal Longitude
    {
        get
        {
            return Math.Round(_longitude, 8);
        }
        set
        {
            this._longitude = value;
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

On this page of the official Microsoft documentation: learn.microsoft.com/en-us/dotnet/standard/base-types/… There is information on how the DataStringFormat modifiers work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.