C#字符串以特定格式浮动

时间:2018-07-30 17:36:19

标签: c# json

我正在尝试转换以下格式的字符串:import numpy as np # Malisiewicz et al. def non_max_suppression_fast(boxes, overlapThresh): if len(boxes) == 0: return [] if boxes.dtype.kind == "i": boxes = boxes.astype("float") pick = [] x1 = boxes[:,0] y1 = boxes[:,1] x2 = boxes[:,2] y2 = boxes[:,3] area = (x2 - x1 + 1) * (y2 - y1 + 1) idxs = np.argsort(y2) while len(idxs) > 0: last = len(idxs) - 1 i = idxs[last] pick.append(i) xx1 = np.maximum(x1[i], x1[idxs[:last]]) yy1 = np.maximum(y1[i], y1[idxs[:last]]) xx2 = np.minimum(x2[i], x2[idxs[:last]]) yy2 = np.minimum(y2[i], y2[idxs[:last]]) w = np.maximum(0, xx2 - xx1 + 1) h = np.maximum(0, yy2 - yy1 + 1) overlap = (w * h) / area[idxs[:last]] idxs = np.delete(idxs, np.concatenate(([last], np.where(overlap > overlapThresh)[0]))) return boxes[pick].astype("int") 转换为30,500(在json中)为浮点型所以目前我有类似30.500的东西,但是当我将其保存在json中时,它另存为float.Parse(string.Format("{0:00,000}", inp_km.Text), CultureInfo.InvariantCulture.NumberFormat)

我在这里做什么错了?

我怎么做到的?

我像这样在30500.00类中创建对象;

Results

现在,当我将新值添加到json(下面的示例)时,我得到的输入是team_results = new Results() { team_number = selected_team.team_number, results = new Result[2] { new Result{ }, new Result { } } };

30,500

但是保存时,它将另存为[ { "team_number": 101, "results": [ { "given_start_time": "20:25", "connection_to_start": "0:00", "start_kp": "20:26", "stop_kp": "0:0", "missed_controls": 0, "km": 0.000, "gtks": [ "25:00", "30:15", "0:00" ] }, { "given_start_time": "21:56", "connection_to_start": "0:00", "start_kp": "21:57", "stop_kp": "0:0", "missed_controls": 0, "km": 0.000, "gtks": [ "25:00", "30:15" ] } ] } ]

3 个答案:

答案 0 :(得分:5)

您正在尝试对一个字符串执行Format,而这只会产生相同的字符串。

您要解析字符串,并将IFormatProvider实现传递给“理解” Parse,含义的.方法在要解析的数字的字符串表示形式中。

在下面的示例中,我使用的文化nl-NL与您在问题中的预期含义相同(.用于分隔成千上万,,用于分隔小数部分)数字)。

const string inputText = "30,500";
var result = float.Parse(inputText, NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.GetCultureInfo("nl-NL"));
Console.WriteLine("Parsed {0} to value {1}", inputText, result);

答案 1 :(得分:2)

您可以创建自定义NumberFormatInfo

var nf = new System.Globalization.NumberFormatInfo();
nf.NumberDecimalSeparator = ",";
nf.NumberGroupSeparator = " ";

并使用它来解析数值

Console.WriteLine(float.Parse("30,5000", nf));

答案 2 :(得分:0)

好吧,我有一个解决方案,但这不是最好的方法,但是它可以工作。 在处理字符串时,可以使用此功能

string converter(string s)
{
s = s.Replace('.',',');
return s;
}

也请检查此链接 https://msdn.microsoft.com/en-us/library/czx8s9ts(v=vs.110).aspx