0

I know this is likely a duplicate question and for that I am sorry. I have been looking for a way to format a custom string, that is pulling values from SQL database. I have read many S.O. on formatting and none have worked.

Ideally I would want the string value to come out something close to $999,888,432.23 (this value is only a placeholder).

Below is one of the ways I have tried to get this string value formatted and I just don't understand why there was 0 changes.

The current string value output is something like 5921.1500000000 (for example; there are always 5-10 trailing 0 that I am trying to remove).

Please let me know if you need more information.

for (int i = 0; i < Sort_List.Count; i++)
{
    Sort_List[i].DollarsOpen = string.Format("{0:0.00}", Sort_List[i].DollarsOpen);
}

Update 1: The following is all the converted data types that is coming from the database. Originally DollarsOpen was a decimal

public class JTPosOpen
{
    public string PoDate;
    public string VendorNo;
    public string Vendor;
    public string PONumber;
    public string POLine;
    public string QtyOrdered;
    public string QtyReceived;
    public string QtyRemaining;
    public string UOM;
    public string ExpUnitCost;
    public string DollarsOpen;
    public string GLAcct;
    public string GLCtr;
    public string JobNo;
    public string ItemNo;
    public string Desc1;
    public string Desc2;
    public string RequestDate;
}
5
  • What is the data type of Sort_List[i].DollarsOpen? Commented Jul 2, 2024 at 18:57
  • @Primdonm: When you are asking question like this you should be more specific. What is you source data type? According to your code the Sort_List[i].DollarsOpen is the string format. Therefore, there is no sense to use numerical pattern like "{0:0.00}" in the Format convertion. Commented Jul 2, 2024 at 19:07
  • I think i have added the requested information. Commented Jul 2, 2024 at 19:13
  • Originally DollarsOpen was a decimal: You should have kept it as a decimal. See Decimal.ToString Method, Standard numeric format strings, and Custom numeric format strings Commented Jul 2, 2024 at 19:24
  • If the value is a string, then you'll have to use string operations (such as String.SubString) to get the desired output. But you shouldn't do this. Store the value as the proper data type (ie: decimal). Commented Jul 2, 2024 at 19:26

1 Answer 1

1

The better data type to save currency is decimal.

Therefore, keep data as decimal and when it is necessary convert it to the string:

decimal price = 123456.4444m;
Sort_List[i].DollarsOpen = $"{price:C}";

The formatted value here will be "$123,456.44".


Perhaps you will need to specify the `CultureInfo` if the current culture in your environment uses the currency symbol different from the `$`:
Sort_List[i].DollarsOpen = price.ToString("C", new System.Globalization.CultureInfo("en-US"));
Sign up to request clarification or add additional context in comments.

Comments

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.