我使用以下XAML将对象绑定到TextBox:
<TextBox Name="MyTextBox" Text="{Binding Path=MyValue, Mode=TwoWay, StringFormat={}{0:F2}}" />
当我绑定一个新对象(其值仍为零)时,Text属性设置为0.00.我有几个TextBox,这使得在输入新值之前删除每个值都很繁琐.
目前我正在Window_Loaded使用FindVisualChildren方法清除方法中的这些框.
但它只是感觉笨重.这样做有一个简洁的方法吗?
我需要将double值格式化为一个小数位而不进行舍入.
double value = 3.984568438706
string result = "";
Run Code Online (Sandbox Code Playgroud)
我试过的是:
1)
result = value.ToString("##.##", System.Globalization.CultureInfo.InvariantCulture) + "%";
// returns 3.98%
Run Code Online (Sandbox Code Playgroud)
2)
result = value.ToString("##.#", System.Globalization.CultureInfo.InvariantCulture) + "%";
// returns 4%
Run Code Online (Sandbox Code Playgroud)
3)
result = value.ToString("##.0", System.Globalization.CultureInfo.InvariantCulture) + "%";
// returns 4.0%
Run Code Online (Sandbox Code Playgroud)
4)(以下其他建议)
value = (value / 100);
result = String.Format("{0:P1}", Math.Truncate(value * 10000) / 10000);
// returns 4.0%
result = string.Format("{0:0.0%}",value); // returns 4.0%
Run Code Online (Sandbox Code Playgroud)
我需要显示的是3.9%的值
在此先感谢您的帮助.
我遇到了一个相当简单的问题,我无法想出一个优雅的解决方案.
我正在str.format一个函数中创建一个字符串,该函数在一个dict替换中传递以用于格式.我想创建字符串并使用值格式化它们,如果它们被传递,否则将它们留空.
防爆
kwargs = {"name": "mark"}
"My name is {name} and I'm really {adjective}.".format(**kwargs)
Run Code Online (Sandbox Code Playgroud)
应该回来
"My name is mark and I'm really ."
Run Code Online (Sandbox Code Playgroud)
而不是抛出KeyError(如果我们不做任何事情会发生什么).
令人尴尬的是,我甚至无法为这个问题提出一个不优雅的解决方案.我想我可以通过不使用来解决这个问题str.format,但如果可能的话,我宁愿使用内置(主要是我想要的).
注意:我事先并不知道将使用哪些密钥.如果有人包含一把钥匙但是没有把它放在kwargs dict中,我试图优雅地失败.如果我100%准确地知道将要查找哪些键,我只需填充所有键并完成它.
我有一个包含了表情符号图标,如代码一些字符串:grinning:,:kissing_heart:或:bouquet:.我想处理它们以删除表情符号代码.
例如,给定:
你好:咧嘴笑:你好吗?:kissing_heart:你还好吗?:花束:
我想得到这个:
你好,你好吗?你还好吗?
我知道我可以使用这段代码:
richTextBox2.Text = richTextBox1.Text.Replace(":kissing_heart:", "").Replace(":bouquet:", "").Replace(":grinning:", "").ToString();
Run Code Online (Sandbox Code Playgroud)
但是,我必须删除856个不同的表情符号图标(使用此方法,将需要856个调用Replace()).有没有其他方法可以实现这一目标?
我log4j v1.2.14用于登录我的项目,我也使用Java 7 String.format()将变量放在我的输出中.目前我正在写作
LOGGER.info(String.format("Your var is [%s] and you are [%s]", myVar, myVar1));
Run Code Online (Sandbox Code Playgroud)
这真的是输出字符串的最佳方式吗?我觉得log4j应该隐式实现如下:
LOGGER.info("Your var is [%s] and you are [%s]", myVar, myVar1);
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?此外,是否有支持这种情况的Java日志框架?
有没有办法在谷歌表格中自定义大数字的格式(两种方式至少最多 10^100):
thousands > K
millions > M
billions > B
trillions > T
etc...
negative quadrillions > Q
decillions > D
Run Code Online (Sandbox Code Playgroud)
可以通过:
10000.1 10.0K
100 100.0
1000 1.0K
10000 10.0K
-100000 -100.0K
45646454 45.6M
5654894844216 5.7T
4655454544 4.7B
46546465455511 46.5T
-46546465455511 -46.5T
4654646545551184854556546454454400000000000000000000000000010000000 4.7U
-1000.9999 -1.0K
-100.8989 -100.9
-20.354 -20.4
1.03 1.0
22E+32 2.2D
Run Code Online (Sandbox Code Playgroud) string-formatting google-sheets number-formatting google-apps-script array-formulas
出于某种原因,当我尝试执行以下操作时出现编译错误:
NSLog(@"row: %lu", indexPath.row);
Run Code Online (Sandbox Code Playgroud)
哪里row是类型NSUInteger.我得到的错误是
转换指定类型'unsigned long'但参数的类型为'NSUInteger'(又名'unsigned int')
我可以执行以下操作,没有编译错误:
NSString * string = [NSString stringWithFormat:@"row: %lu", indexPath.row];
Run Code Online (Sandbox Code Playgroud)
我使用的是在这两种情况下完全相同的格式字符串和替换参数,但为什么NSLog吓坏了,而-stringWithFormat:似乎是完美的内容?我的编译器是LLVM 1.6.
我试图以优雅的方式将当前时间作为"YYYY-MM-DD-HH-MM-SS"格式化字符串.我可以从Boost的"Date Time"库获取ISO格式的当前时间,但它有其他分隔字符串,这对我不起作用(我在文件名中使用它).当然我可以直接替换分隔字符串,但感觉有一种更好的方法可以使用日期时间的格式化选项.有这样的方式,如果有的话,我该如何使用它?
这基本上就是我想要做的
// ... some code, calculations, what have you ...
long timeToAdd = returnTimeToAddInLongFormat();
// lets output the long type now, and yes i need the width and precision.
System.out.printf("Time to add: %13.10ld", timeToAdd);
Run Code Online (Sandbox Code Playgroud)
我已经阅读了围绕这个主题的大部分谷歌搜索,并认为我理解如何在概念上做到这一点,但JRE不断抛弃我UnknownFormatConversionException并告诉我我的输入大小修改器l不起作用.
还有另一种方法可以做到这一点,还是我想念一些小事?
视图:
<TextBlock Text="{Binding Date}"/>
Run Code Online (Sandbox Code Playgroud)
我希望将日期格式化为"dd/MM/yyyy",换句话说,没有时间.
我试过了:<TextBlock Text="{Binding Date, StringFormat={}{0:dd/MM/yyyy}}"/>但它不起作用.
给我一个错误:在'Binding'类型中找不到属性'StringFormat'.