1

I want format this ActualSize property into Non Decimal point but have a comma separated. Eg. I have the value 1000.65 convert it into 1,001 and not 1,000

Example:

public class Price 
{
    public int Id { get; set; }
    public string Name { get; set; }

    // I want format this ActualSize property into Non Decimal point but have a comma separated. Eg. I have the value 1000.65 convert it into 1,001 and not 1,000 
    public long? ActualSize { get; set; }
}

public List<Product> ListProduct()
{
    List<Product> listProducts = new List<Product>();

    listProducts.Add(new Product {Id = "1", Name = "Product 1" ActualSize = 1000.65 });
    listProducts.Add(new Product {Id = "2", Name = "Product 2", ActualSize = 2443.76 });
    return listProducts;
}

3 Answers3

0

Hope that this is what you are looking for:

string formatedOp = 2319000.6.ToString("#,##,##"); // 2,319,001

Here is a working example.

But still I don't get what you are going to deal with long? if this for any specific reason means please include the reason so that I can update my answer. anyway you cannot store values with commas in long but you can print them with commas as like what I did in the above example.

Updates as per modified question: So your input contains some double values and you want them as in the specified format. I can suggest an option for this don't know whether it help you or not. anyway you can make a try:

Make the following changes in the properties: - change long? to double? as your input is double. - Include a readonly string property in the class which returns the specific string. - Which will be updated when you set the double value. In short the class will looks like the following after applying the following changes:

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    private string _ActualSizeStr;
    private double? _ActualSize;

    public string ActualSizeStr
    {
        get { return _ActualSizeStr; }
    }

    public double? ActualSize
    {
        get { return _ActualSize; }
        set
        {
            _ActualSize = value;
            if (value.HasValue)
            {
                _ActualSizeStr = value.Value.ToString("#,##,##");
            }
        }
    }

}

So when you assign a value(let it be 2319000.6) for ActualSize the _ActualSizeStr will be updated with the required value(2,319,001) so that when you retrieve or use those values make use of ActualSizeStr instead for ActualSize.

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
0

You can use String.Format() like this:

string areaSize = String.Format("{0:n0}", AreaSize);

Jaime Macias
  • 857
  • 6
  • 11
  • Thank you, but how to apply it in property? Honestly, I use it in LINQ which is not supported. – Stephen Shuane Feb 10 '17 at 01:20
  • @StephenShuane It is, it will just require 1 additional step. First you "get" everything as string with `ToList()` and then in next step you can use this Format function. – Keyur PATEL Feb 10 '17 at 01:26
0

You need to format the string in using a .ToString overload.

If you need the spaces you need a custom format string, as there is nothing built into .NET that does exactly what you have specified.

I actually for some reason thought there was a comma format built into .NET but I don't see it, if you don't need the spaces the following works:

1234897.11m.ToString("{0,0}}");

However if you do need the spaces it is much more a pain (thank you to this post for the answer).

using System;
using System.Globalization;
var nfi = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone();
nfi.NumberGroupSeparator = " ,";
1234897.11m.ToString("#,0", nfi);

I just noticed you want it inside your property:

public string FormatedAreaSize
{
   get{return this.AreaSize?.ToString(...);
}
Community
  • 1
  • 1
Jason Lind
  • 263
  • 1
  • 2
  • 15
  • see for this is my code: http://stackoverflow.com/questions/42150774/why-linq-query-and-extension-dont-support-conversion-or-rounding-numbers – Stephen Shuane Feb 10 '17 at 03:04