Share via

How to use commas after three digits next to numbers using LINQ Query

jewel 1,231 Reputation points
2024-12-25T14:27:43.9966667+00:00
I want to separate the numeric values ​​in my query with commas for ease of viewing. Is it possible? example
1000.00 I want to see 1,000.00
10000.00 I want to see 10,000.00
100000.00 I want to see 100,000.00

//Query
  var data = (from a in _context.tbl_BankTransactions
              join b in _context.tbl_Banks
              on a.bankid equals b.bankid
            
              where a.Date >= _fitsdate
              where a.Date <= _lastdate
              select new
              {
                  bankTransctionid = a.bankTransctionid,
                  BankName = b.BankName,
                  AccountNo = b.AccountNo,
                  debited = (decimal?)a.debited ?? 0,
                  credited = (decimal?)a.credited ?? 0,
                  Date = a.Date
            
              }).ToList();

I want to see this data debited & credited value commas after three digits
Developer technologies | ASP.NET Core | Other
0 comments No comments

Answer accepted by question author

  1. Anonymous
    2024-12-26T03:32:38.1766667+00:00

    Hi jewel,

    You could use custom numeric format strings https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings#the--custom-specifier-2

    debited = ((decimal?)a.debited ?? 0).ToString("#,##0.00"),
    credited = ((decimal?)a.credited ?? 0).ToString("#,##0.00"),
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".  Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. SurferOnWww 5,941 Reputation points
    2024-12-26T02:20:09.89+00:00

    This answer has been deleted because of duplication.

    Was this answer helpful?

    0 comments No comments

  2. SurferOnWww 5,941 Reputation points
    2024-12-26T02:19:24.72+00:00

    How to use commas after three digits next to numbers using LINQ Query

    The type of debited and credited is decimal?. You cannot see them in "commas after three digits" format as long as type is decimal (same for short, int, long, float and double). Linq does not matter.

    To see the values of debited and credited in "commas after three digits" format you will have to convert them to string with proper formatting.

    Please refer to the following Microsoft documents for details of standard and cusrom numeric format strings:

    Standard numeric format strings

    Custom numeric format strings

    Was this answer helpful?

    0 comments No comments

  3. Bruce (SqlWork.com) 83,981 Reputation points
    2024-12-25T16:25:34.8666667+00:00

    debited & credited are binary values. You don’t show the code that displays them. In this code you should add formatting. Or convert to strings in the query

    select new
                  {
                      bankTransctionid = a.bankTransctionid,
                      BankName = b.BankName,
                      AccountNo = b.AccountNo,
                      debited = ((decimal?)a.debited ?? 0).ToString(“D”),
                      credited = ((decimal?)a.credited ?? 0).ToString(“D”),
                      Date = a.Date
                
                  })
    

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.