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

      <tfoot id='lNsLG'></tfoot>

        <legend id='lNsLG'><style id='lNsLG'><dir id='lNsLG'><q id='lNsLG'></q></dir></style></legend>

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

          <bdo id='lNsLG'></bdo><ul id='lNsLG'></ul>
      1. 如何使用适当的文化信息将字符串转换为双精度

        时间:2023-08-27
          <bdo id='nKyy7'></bdo><ul id='nKyy7'></ul>

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

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

                    <tbody id='nKyy7'></tbody>
                  本文介绍了如何使用适当的文化信息将字符串转换为双精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我在数据库中有两个 nvarchar 字段来存储 DataType 和 DefaultValue,我有一个 DataType Double 和值为 65.89875 的英文格式.

                  I have two nvarchar fields in a database to store the DataType and DefaultValue, and I have a DataType Double and value as 65.89875 in English format.

                  现在我希望用户根据选择的浏览器语言格式查看值(英语中的 65.89875 应显示为德语中的 65,89875).现在,如果用户从德语格式编辑为 65,89875,相当于英语中的 65.89875,而其他用户从英文浏览器查看,则为 6589875.

                  Now I want the user to see the value as per the selected browser language format (65.89875 in English should be displayed as 65,89875 in German). Now if the user edits from German format to 65,89875 which is 65.89875 equivalent in English, and the other user views from an English browser it comes as 6589875.

                  发生这种情况是因为在数据库中它在 nvarchar 列中存储为 65,89875,当使用英语文化转换时,它变为 6589875,因为它将 , 视为分隔符,这是德语的小数运算符.

                  This happens because in the database it was stored as 65,89875 in the nvarchar column and when converted using English culture it becomes 6589875 since it considers , as a separator which is a decimal operator for German.

                  如何让它适用于所有浏览器?

                  How do I get this working for all the browsers?

                  推荐答案

                  您需要为存储在数据库中的数据定义一个区域设置,不变的文化就是为了这个目的.

                  You need to define a single locale that you will use for the data stored in the database, the invariant culture is there for exactly this purpose.

                  当你显示转换为本机类型,然后为用户的文化格式化.

                  When you display convert to the native type and then format for the user's culture.

                  例如显示:

                  string fromDb = "123.56";
                  string display = double.Parse(fromDb, CultureInfo.InvariantCulture).ToString(userCulture);
                  

                  存储:

                  string fromUser = "132,56";
                  double value;
                  // Probably want to use a more specific NumberStyles selection here.
                  if (!double.TryParse(fromUser, NumberStyles.Any, userCulture, out value)) {
                    // Error...
                  }
                  string forDB = value.ToString(CultureInfo.InvariantCulture);
                  

                  PS.几乎不用说,使用具有与数据匹配的数据类型的列会更好(但有时也适用于旧版).

                  PS. It, almost, goes without saying that using a column with a datatype that matches the data would be even better (but sometimes legacy applies).

                  这篇关于如何使用适当的文化信息将字符串转换为双精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:忽略字符串比较中的重音字母 下一篇:通常最好使用哪个 - StringComparison.OrdinalIgnoreCase

                  相关文章

                  最新文章

                    <legend id='jnZgR'><style id='jnZgR'><dir id='jnZgR'><q id='jnZgR'></q></dir></style></legend>
                  1. <i id='jnZgR'><tr id='jnZgR'><dt id='jnZgR'><q id='jnZgR'><span id='jnZgR'><b id='jnZgR'><form id='jnZgR'><ins id='jnZgR'></ins><ul id='jnZgR'></ul><sub id='jnZgR'></sub></form><legend id='jnZgR'></legend><bdo id='jnZgR'><pre id='jnZgR'><center id='jnZgR'></center></pre></bdo></b><th id='jnZgR'></th></span></q></dt></tr></i><div id='jnZgR'><tfoot id='jnZgR'></tfoot><dl id='jnZgR'><fieldset id='jnZgR'></fieldset></dl></div>
                    • <bdo id='jnZgR'></bdo><ul id='jnZgR'></ul>
                  2. <small id='jnZgR'></small><noframes id='jnZgR'>

                  3. <tfoot id='jnZgR'></tfoot>