【问题标题】:decimal value displays as fraction, too many spaces [duplicate]十进制值显示为分数,空格太多[重复]
【发布时间】:2020-03-21 06:25:01
【问题描述】:

我有这个代码:

    document.getElementById('numsco').value = ($.trim(data.nscore));

ncore 的值为 140.25。它显示为:

140  2/8

出于排序目的,它需要显示为:

140 2/8

有没有办法迫使多余的空间消失?

replace(" ", " ") 不起作用,我在网上找到的几个正则表达式解决方案中的任何一个都不起作用。

这不是上述问题的重复,因为我在这里使用了该代码:

document.getElementById('numsco').value = ($.trim(data.nscore));
document.getElementById('numsco').value.replace(/\s/g, "");
alert(document.getElementById('numsco').value);

它会返回这个:

问题似乎与用于显示数据的任何工具的方式有关。

这是 Razor 代码:

<td hidden="hidden">@Html.EditorFor(model => model.tblMeasurement.num, new { htmlAttributes = new { @class = "form-control", id = "numsco", @readonly = "readonly", tabindex = "58000", Style = "border-color:none" } })</td>

找到了修复,如果不是真正的解决方案:

最初的程序员写了这个怪物:

            List<tblReglist> reglist = new List<tblReglist>();

            var cnty = (from a in be.FIPSCountyCodes
                        where a.StateCode == 47
                        select new
                        {
                            county = a.CountyName,
                            countyid = a.CountyCode,
                            state = a.StateCode
                        }).ToArray();


            var regtbl = (from reg in db.tblReglists
                          join mes in db.tblMeasurements
                          on reg.ID equals mes.ID
                          where reg.IsUsed == true
                          && (mes.IsUsed == true)
                          && (reg.regeligible == "Y")
                          && (mes.IsUsed == true)
                          //restrictions
                          && (reg.weapon.Contains("Bow") && (mes.typ.Contains("Typ") && mes.numScore >= 115)
                          || (reg.weapon.Contains("Bow") && (mes.typ.Contains("NonTyp") && mes.numScore >= 140))
                          || (reg.weapon.Contains("Muzzleloader") && (mes.typ.Contains("NonTyp") && mes.numScore >= 150))
                          || (reg.weapon.Contains("Muzzleloader") && (mes.typ.Contains("Typ") && mes.numScore >= 125))
                          || (reg.weapon.Contains("Gun") && (mes.typ.Contains("Typ") && mes.numScore >= 140))
                          || (reg.weapon.Contains("Gun") && (mes.typ.Contains("NonTyp") && mes.numScore >= 165)))
                          orderby mes.numScore descending
                          select new
                          {
                              scored = mes.abtotal8,
                              type = mes.typ,
                              weapons = reg.weapon,
                              county = reg.CountyId,
                              harvestdate = reg.harvestDate,
                              frstname = reg.firstName,
                              lstname = reg.lastName,
                              nscre = mes.numScore

                          }).ToArray();

            var query = from r in regtbl
                        join c in cnty
                        on r.county equals c.countyid
                        orderby r.nscre descending
                        select new
                        {
                            firstName = r.frstname,
                            score = r.scored,
                            typ = r.type,
                            weapon = r.weapons,
                            countys = c.county,
                            harvdate = r.harvestdate,
                            lstnme = r.lstname
                        };


            foreach (var item in query)
            {
                reglist.Add(new tblReglist()
                {
                    city = item.score.ToString(),
                    street = item.typ,
                    weapon = item.weapon,
                    state = item.countys,
                    firstName = item.firstName,
                    lastName = item.lstnme,
                    harvestDate = item.harvdate
                });
            }

            return View(reglist);

这是一团糟透了的意大利面,但在几个同事的帮助下,我们跟进并发现它与 ToString() 函数在此处修改值的方式有关:

city = item.score.ToString()

添加

.Replace("  ", " ")

...在 ToString() 使其正确显示之后。

鄙视在其他程序员的代码上工作。

【问题讨论】:

  • 您从哪里获得价值?顺便说一句,如果你有'1/4',为什么要'2/8'
  • @NinaScholz 我从数据库中获取值,它的值一直到上面的代码都是 140.25。至于'2/8',嗯,我继承的时候就是这样,显然这就是这个州多年来对鹿角得分的方式。
  • 为了让我们为您提供更好的解决方法,您应该让我们了解如何获得每个结果。这意味着,data.score 包含什么,您是否使用任何其他函数来获取当前结果?等等。
  • @k3llydev -- 我所有围绕这个字段的代码现在都在问题中。

标签: javascript razor-pages


【解决方案1】:

replace(" ", " ") 将替换每个空格.. 用空格。您可以用一个空格替换任意数量的空格:

let fraction = "140  2/8"
fraction.replace(/ +/g, ' ') // '140 2/8'

/ +/g 是一个全局匹配一个或多个空格的正则表达式。在 replace 方法中,这 1 个(或更多)空格被一个替换,基本上将空格的数量截断为一个。

【讨论】:

  • 我原文有两个空格;它只是无法在网站上正确显示,因为我没有输入  添加额外的。
猜你喜欢
  • 2021-10-10
  • 1970-01-01
  • 1970-01-01
  • 2019-11-22
  • 1970-01-01
  • 2018-01-28
  • 1970-01-01
  • 2020-06-19
  • 1970-01-01
相关资源
最近更新 更多