简体   繁体   English

string.format-逗号作为小数点分隔符

[英]string.format - Commas as decimal separator

Given the input 123.45 , I'm trying to get the output 123,45 via String.Format . 给定输入123.45 ,我试图通过String.Format获取输出123,45

Because of the system I'm working in, the actual format strings (eg {0:0.00} ) are saved in a config file and transformed later down the pipeline. 由于我正在使用的系统,实际的格式字符串(例如{0:0.00} )保存在配置文件中,并在以后沿管道进行转换。

Editing the config file to add a new format is "safe" and I can get this through quite quickly. 编辑配置文件以添加新格式是“安全的”,我可以很快地做到这一点。 Editing the actual parser later down the line is "risky" so will need a more significant QA resource and I need to avoid this. 稍后在行中编辑实际的解析器是“高风险的”,因此将需要更重要的质量检查资源,我需要避免这种情况。

As a result, some caveats: 结果,一些警告:

  • I only have access to string.Format(pattern, input) . 我只能访问string.Format(pattern, input) No overloads. 没有过载。
  • I cannot send a localisation. 我无法发送本地化信息。 I know that if I send string.Format(new System.Globalization.CultureInfo("de-DE"), "{0:0.00}", 123.45) then I've got what I need. 我知道,如果我发送string.Format(new System.Globalization.CultureInfo("de-DE"), "{0:0.00}", 123.45)那么我string.Format(new System.Globalization.CultureInfo("de-DE"), "{0:0.00}", 123.45)了所需的东西。 But I cannot pass the localisation. 但是我无法通过本地化。

So can it be done? 这样可以吗?
Is there any format I can pass to string.Format which will transform 123.45 into 123,45 ? 我可以将任何格式传递给string.Format并将123.45转换为123,45吗?

If you can you multiply the input by 100 then the following should work: 如果您可以将输入乘以100,则应该可以进行以下操作:

double input = 123.45;
string pattern = "{0:###\\,##}";

var result = String.Format(pattern, input*100);

Just for the fun of it :) 就是图个好玩儿 :)

        double value =123.45;
        Console.WriteLine(String.Format("{0:#0.00}\b\b\b,", value));

This of course only works when there is a cursor, like in the console, otherwise the backspace control characters have no effect. 当然,这仅在有光标的情况下才有效,例如在控制台中,否则退格控制字符无效。

Sorry, but i can't think of a real way in accomplishing this. 抱歉,但是我想不出真正的方法来实现这一目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM