<bdo id='DLMDS'></bdo><ul id='DLMDS'></ul>
    <legend id='DLMDS'><style id='DLMDS'><dir id='DLMDS'><q id='DLMDS'></q></dir></style></legend>

    <small id='DLMDS'></small><noframes id='DLMDS'>

        <i id='DLMDS'><tr id='DLMDS'><dt id='DLMDS'><q id='DLMDS'><span id='DLMDS'><b id='DLMDS'><form id='DLMDS'><ins id='DLMDS'></ins><ul id='DLMDS'></ul><sub id='DLMDS'></sub></form><legend id='DLMDS'></legend><bdo id='DLMDS'><pre id='DLMDS'><center id='DLMDS'></center></pre></bdo></b><th id='DLMDS'></th></span></q></dt></tr></i><div id='DLMDS'><tfoot id='DLMDS'></tfoot><dl id='DLMDS'><fieldset id='DLMDS'></fieldset></dl></div>

        <tfoot id='DLMDS'></tfoot>

        如何将百分比字符串转换为双倍?

        How to convert percentage string to double?(如何将百分比字符串转换为双倍?)

        <small id='9gsVS'></small><noframes id='9gsVS'>

          <legend id='9gsVS'><style id='9gsVS'><dir id='9gsVS'><q id='9gsVS'></q></dir></style></legend>
          <i id='9gsVS'><tr id='9gsVS'><dt id='9gsVS'><q id='9gsVS'><span id='9gsVS'><b id='9gsVS'><form id='9gsVS'><ins id='9gsVS'></ins><ul id='9gsVS'></ul><sub id='9gsVS'></sub></form><legend id='9gsVS'></legend><bdo id='9gsVS'><pre id='9gsVS'><center id='9gsVS'></center></pre></bdo></b><th id='9gsVS'></th></span></q></dt></tr></i><div id='9gsVS'><tfoot id='9gsVS'></tfoot><dl id='9gsVS'><fieldset id='9gsVS'></fieldset></dl></div>
            <tbody id='9gsVS'></tbody>

            • <bdo id='9gsVS'></bdo><ul id='9gsVS'></ul>

              <tfoot id='9gsVS'></tfoot>

                1. 本文介绍了如何将百分比字符串转换为双倍?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一个像1.5%"这样的字符串,想把它转换成双精度值.

                  I have a string like "1.5%" and want to convert it to double value.

                  可以通过以下方式简单地完成:

                  It can be done simple with following:

                  public static double FromPercentageString(this string value)
                  {
                      return double.Parse(value.SubString(0, value.Length - 1)) / 100;
                  }
                  

                  但我不想使用这种解析方式.

                  but I don't want to use this parsing approach.

                  还有其他方法可以使用 IFormatProvider 或类似的方法吗?

                  Is any other approach with IFormatProvider or something like this?

                  推荐答案

                  如果您关心捕获格式错误,我会使用 TrimEnd 而不是 Replace.替换将允许格式错误通过而不被发现.

                  If you care about catching formatting errors, I would use TrimEnd rather than Replace. Replace would allow formatting errors to pass undetected.

                  var num = decimal.Parse( value.TrimEnd( new char[] { '%', ' ' } ) ) / 100M;
                  

                  这将确保该值必须是某个十进制数字,后跟任意数量的空格和百分号,即,它必须至少以正确格式的值开头.更准确地说,您可能希望在%"上进行拆分,而不是删除空条目,然后确保只有两个结果,第二个是空的.第一个应该是要转换的值.

                  This will ensure that the value must be some decimal number followed by any number of spaces and percent signs, i.e, it must at least start with a value in the proper format. To be more precise you might want to split on '%', not removing empty entries, then make sure that there are only two results and the second is empty. The first should be the value to convert.

                  var pieces = value.Split( '%' );
                  if (pieces.Length > 2  || !string.IsNullOrEmpty(pieces[1]))
                  { 
                      ... some error handling ... 
                  }
                  var num = decimal.Parse( pieces[0] ) / 100M;
                  

                  使用 Replace 将允许您成功并错误地 IMO 解析以下内容:

                  Using Replace will allow you to successfully, and wrongfully IMO, parse things like:

                  • %1.5
                  • 1%.5
                  • 1.%5

                  除了1.5%

                  这篇关于如何将百分比字符串转换为双倍?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
                  Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
                  Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
                  How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
                  how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
                  JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)

                  <small id='cIvMx'></small><noframes id='cIvMx'>

                    <tbody id='cIvMx'></tbody>
                  <i id='cIvMx'><tr id='cIvMx'><dt id='cIvMx'><q id='cIvMx'><span id='cIvMx'><b id='cIvMx'><form id='cIvMx'><ins id='cIvMx'></ins><ul id='cIvMx'></ul><sub id='cIvMx'></sub></form><legend id='cIvMx'></legend><bdo id='cIvMx'><pre id='cIvMx'><center id='cIvMx'></center></pre></bdo></b><th id='cIvMx'></th></span></q></dt></tr></i><div id='cIvMx'><tfoot id='cIvMx'></tfoot><dl id='cIvMx'><fieldset id='cIvMx'></fieldset></dl></div>
                  <legend id='cIvMx'><style id='cIvMx'><dir id='cIvMx'><q id='cIvMx'></q></dir></style></legend>

                    • <bdo id='cIvMx'></bdo><ul id='cIvMx'></ul>
                        1. <tfoot id='cIvMx'></tfoot>