我注意到C#/ .NET中存在以下不一致之处.我想知道为什么会这样.
Console.WriteLine("{0,-4:#.0} | {1,-4:#.0}", 1.04, Math.Round(1.04, 1));
Console.WriteLine("{0,-4:#.0} | {1,-4:#.0}", 1.05, Math.Round(1.05, 1));
Console.WriteLine("{0,-4:#.0} | {1,-4:#.0}", 1.06, Math.Round(1.06, 1));
Console.WriteLine("{0,-4:#.0} | {1,-4:#.0}", 1.14, Math.Round(1.14, 1));
Console.WriteLine("{0,-4:#.0} | {1,-4:#.0}", 1.15, Math.Round(1.15, 1));
Console.WriteLine("{0,-4:#.0} | {1,-4:#.0}", 1.16, Math.Round(1.16, 1));
Console.WriteLine();
Console.WriteLine("{0,-4:#.0} | {1,-4:#.0}", 1.04, Math.Round(1.04, 1, MidpointRounding.AwayFromZero));
Console.WriteLine("{0,-4:#.0} | {1,-4:#.0}", 1.05, Math.Round(1.05, 1, MidpointRounding.AwayFromZero));
Console.WriteLine("{0,-4:#.0} | {1,-4:#.0}", 1.06, Math.Round(1.06, 1, MidpointRounding.AwayFromZero));
Console.WriteLine("{0,-4:#.0} | {1,-4:#.0}", 1.14, Math.Round(1.14, 1, MidpointRounding.AwayFromZero));
Console.WriteLine("{0,-4:#.0} | {1,-4:#.0}", 1.15, Math.Round(1.15, 1, MidpointRounding.AwayFromZero));
Console.WriteLine("{0,-4:#.0} | {1,-4:#.0}", 1.16, Math.Round(1.16, 1, …Run Code Online (Sandbox Code Playgroud) 我有一个字符串
string str ="Enter {0} patient name";
Run Code Online (Sandbox Code Playgroud)
我正在使用string.format来格式化它.
String.Format(str, "Hello");
Run Code Online (Sandbox Code Playgroud)
现在,如果我想要从一些配置中检索患者,那么我需要将str改为类似的东西
"Enter {0} {1} name".所以它将用第二个值替换{1}.问题是我想要而不是{1}其他一些格式{pat}.但是当我尝试使用时,它会抛出一个错误.我想要一种不同格式的原因是我需要更改这么多文件(可能包含{0},{1}等).所以我需要一个可以在运行时替换的自定义占位符.
我似乎找不到一个简单的方法来做到这一点.我需要的确切事项是:
[NSString stringWithFormat:@"%d doodads", n];
Run Code Online (Sandbox Code Playgroud)
其中n是int.因此对于1234我想要这个字符串(在我的语言环境下):
@"1,234 doodads"
Run Code Online (Sandbox Code Playgroud)
谢谢.
当我使用字符串格式时,我可以多次访问一个参数而不再传递它吗?
例:
NSString *parameter1 = @"1";
NSString *parameter2 = @"2";
NSString *myString;
myString = [NSString stringWithFormat:@"I want to print parameter1 here: %@, parameter2 here: %@ and now access parameter1 again: %@ _without_ passing it again.",parameter1, parameter2, parameter1];
Run Code Online (Sandbox Code Playgroud)
有没有办法再次访问第一个参数而不再写",parameter1"?
现在(C#4.0)我们的日志记录方法看起来像
public void Log(string methodName, string messageFormat, params object[] messageParameters)
Run Code Online (Sandbox Code Playgroud)
记录器执行字符串格式化的位置,以便调用者没有放置String.Format来创建一个很好的日志消息(如果没有附加logviewer,则允许记录器跳过字符串格式).
使用c#5.0我想通过使用新的CallerMemberName属性来摆脱methodName参数,但我不知道如何将它与'params'关键字结合起来.有没有办法做到这一点?
基本上我正在格式化这样的数字
@String.Format("{0:C}", Model.Price)
Run Code Online (Sandbox Code Playgroud)
结果是$ 2,320,000.00
然而,我想要的结果分别是2,320,000.00没有$.我怎样才能实现这一点,同时仍然利用.nets本地化处理?
编辑
如果我想改变我的课程,那么当一个人试图获得时Price,他会得到这种格式化的价格.这是我的课
public class Listing : DomainObject
{
public decimal Price { get; set; }
public decimal Commission { get; set; }
}
Run Code Online (Sandbox Code Playgroud) 我想知道是否可以格式化Javascript模板字符串中的数字,例如:
var n = 5.1234;
console.log(`This is a number: $.2d{n}`);
// -> 5.12
Run Code Online (Sandbox Code Playgroud)
或者可能
var n = 5.1234;
console.log(`This is a number: ${n.toString('.2d')}`);
// -> 5.12
Run Code Online (Sandbox Code Playgroud)
这种语法显然不起作用,它只是我正在寻找的事物类型的一个例子.
我知道像sprintffrom underscore.string这样的工具,但这似乎是JS应该能够开箱即用的东西,特别是考虑到模板字符串的强大功能.
编辑
如上所述,我已经知道第三方工具(例如sprintf)和自定义功能.类似的问题(例如,相当于printf/String.Format的JavaScript)根本没有提到模板字符串,可能是因为在ES6模板字符串出现之前就被问过了.我的问题是针对ES6的,并且与实施无关.我很高兴接受一个答案"不,这是不可能的",如果是这样的话,但是最好的是有关提供此功能的新ES6功能的信息,或者是否有关于此功能是否在其上的一些信息办法.
对于我目前正在进行的项目,我必须将特殊格式的字符串提供给第三方服务进行处理.所以我正在建立这样的字符串:
string someString = string.Format("{0}{1}{2}: Some message. Some percentage: {3}%", token1, token2, token3, number);
Run Code Online (Sandbox Code Playgroud)
而不是硬编码字符串,我正在考虑将其移动到项目资源中:
string someString = string.Format(Properties.Resources.SomeString, token1, token2, token3, number);
Run Code Online (Sandbox Code Playgroud)
在我看来,第二种选择并不像第一种选择那样可读,即阅读代码的人必须提取字符串资源以确定最终结果应该是什么样子.
我该如何解决这个问题?在这种情况下,硬编码格式字符串是否是必要的恶魔?
我通过DataContext 将Amount Label的 Content属性绑定到十进制属性.我正在尝试应用stringformat但看不到任何效果.StringFormat功能是否适用于Label控件?请告诉我这项功能适用于哪些控件.BTW以下是我想要应用货币格式的Label Control代码
<Label Grid.Column="2" Content="{Binding Path=Amount, StringFormat={}{0:C}}" Height="23" HorizontalAlignment="Left" Margin="100,10,0,0" Name="tb" VerticalAlignment="Bottom" Width="120" />
Run Code Online (Sandbox Code Playgroud) 在fortran中,我能够重复格式描述符以保存多次重写,例如:
write(*,'(i5,i5,i5,i5,i5)')a,b,c,d,e
Run Code Online (Sandbox Code Playgroud)
可以改写为
write(*,'(5(i5))')a,b,c,d,e
Run Code Online (Sandbox Code Playgroud)
可以在python中使用类似的方法吗?
例如,假设我想在python中执行相同的操作,我必须写:
print "{0:5d} {1:5d} {2:5d} {3:5d} {4:5d}".format(a,b,c,d,e)
Run Code Online (Sandbox Code Playgroud)
有没有办法重复格式描述符,比如fortran?
c# ×5
.net ×2
c#-5.0 ×1
cocoa ×1
ecmascript-6 ×1
javascript ×1
label ×1
localization ×1
math ×1
nsstring ×1
objective-c ×1
python ×1
resources ×1
rounding ×1
string ×1
wpf ×1
xaml ×1