C# Truncating a string to 1 or 2 places after decimal without converting?

huangapple go评论88阅读模式
英文:

C# Truncating a string to 1 or 2 places after decimal without converting?

问题

The code you provided is in C# and it appears that you're looking for a way to truncate decimal values in certain strings to two decimal places. To achieve this without converting to a decimal and back, you can use the following one-liner for those specific lines:

items[x] = $"{items[x]:F2}";

This will format the string in items[x] to have exactly two decimal places without needing to convert it to a decimal type.

Please note that this line should replace the lines like newcomp.Range = items[2].Substring(0, items[2].IndexOf('.')) + 3); for the six values you mentioned.

英文:

I am receiving a csv string that I need to display in a .Net6.0 wpf ListView. I'm processing thousands of strings per second, and would like a quick way to truncate a string after the decimal for six of the values in the string. I know that I can convert to a number, but this seems bulky, especially since I would have to convert back to string for display. Here is my function:

public void AddComponent(string component)
{
    Component newcomp = new Component();
    string[] items = component.Split(',');
    newcomp.Time = items[0].Substring(items[0].IndexOf(':') + 1);
    newcomp.CompId = items[1];
    newcomp.Range = items[2].Substring(0, items[2].IndexOf('.') + 3);
    newcomp.Diameter = items[3].Substring(0, items[3].IndexOf('.') + 3);
    newcomp.Radius = items[4].Substring(0, items[4].IndexOf('.') + 3);
    newcomp.Height = items[5];
    newcomp.Validity = EnumExtensions.GetDescription((ComponentValidity)(ushort.Parse(items[6])));
    newcomp.Value = items[7];
    newcomp.RCS = items[8];
    newcomp.Status = EnumExtensions.GetDescription((ComponentStatus)ushort.Parse(items[9].Substring(0, 1)));
    newcomp.Confidence = EnumExtensions.GetDescription((ComponentConfidence)ushort.Parse(items[9].Substring(3, 1)));
    newcomp.MNF = items[10].Substring(0, items[10].IndexOf('.') + 3);
    newcomp.Group = items[11];
    newcomp.Classification = EnumExtensions.GetDescription((ComponentClassification)(ushort.Parse(items[12])));
    newcomp.Serial = items[13];
    newcomp.ShelfNumber = items[14].Substring(0, items[14].IndexOf('.') + 3);
    newcomp.HYT = items[15].Substring(0, items[15].IndexOf('.') + 3);
    Components.Add(newcomp);
}

The main ones I'm concerned about are the six that do this:

items[x].Substring(0, items[x].IndexOf('.') + 3);

These numbers can look like:

34.2
34.24
34.2456
76765.2232

I want the end result to be

34.2
34.24
34.24
76765.22

And of course the problem is the + 3 on the end of the line because sometimes there aren't enough characters to fill that out.

I'm already parsing some of the items to get the enum values, so I would rather not convert these strings to a decimal and back again. Is there an easy one-liner that might accomplish this?

答案1

得分: 0

我找到了一种很好用的两行代码:

int index = items[2].IndexOf('.');
newcomp.Range = items[2].Substring(0, index + (items[2].Length - index > 3 ? 3 : items[2].Length - index));

编辑

将Matthew Watson的示例与这两行代码结合起来,并考虑到可能没有小数点,我得到了以下代码:

int index = items[2].IndexOf('.');
newcomp.Range = index == -1 ? items[2] : items[2][..Math.Min(items[2].Length, index + 3)];
英文:

I figured out a two-liner that works pretty well:

int index = items[2].IndexOf('.');
newcomp.Range = items[2].Substring(0, index + (items[2].Length - index > 3 ? 3 : items[2].Length - index));

EDIT

Combining Matthew Watson's samples with this two-line approach, and taking into account there may not be a decimal point at all, I get:

int index = items[2].IndexOf('.');
newcomp.Range = index == -1 ? items[2] : items[2][..Math.Min(items[2].Length, index + 3)];

答案2

得分: 0

也许是这样的?
var tempNumber = items[x] + "0"; tempNumber.Substring(0, tempNumber.IndexOf('.') + 3);

英文:

Perhaps something like this?
var tempNumber = items[x]+"0";
tempNumber.Substring(0, itempNumber.IndexOf('.') + 3);

huangapple
  • 本文由 发表于 2023年6月8日 21:30:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76432379.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定