我想知道最简单的方法是将整数转换为等效的空格数.打印二叉搜索树时,我需要它用于节点之间的空间.我试过这个
int position = printNode.getPosition();
String formatter = "%1"+position+"s%2$s\n";
System.out.format(formatter, "", node.element);
Run Code Online (Sandbox Code Playgroud)
但是,与位置的int值相比,我得到的空间几乎是3倍.我不确定我是否正在格式化字符串.任何建议都会很棒!如果它更清楚,比如position = 6; 我想在节点元素之前打印6个空格.
在这篇关于SQLite的帖子中,aaronasterling告诉我
cmd = "attach \"%s\" as toMerge" % "b.db" : 是错的cmd = 'attach "{0}" as toMerge'.format("b.db") : 是正确的cmd = "attach ? as toMerge"; cursor.execute(cmd, ('b.db', )) :是对的但是,我认为第一和第二是相同的.这三者有什么不同?
如何在数据不存在时隐藏字符串格式.请考虑此示例
<TextBlock Text="{Binding Amount, StringFormat=Total: {0:C}}" />
Run Code Online (Sandbox Code Playgroud)
在这种情况下,如果Amount为null,那么它将只显示Total:.如果Amount为null或为空,如何隐藏它
没找到怎么做.我发现的或多或少就是这个(http://blog.stevex.net/string-formatting-in-csharp/):
除了它的对齐之外,字符串中确实没有任何格式.对齐适用于在String.Format调用中打印的任何参数.样本生成
String.Format(“->{1,10}<-”, “Hello”); // gives "-> Hello<-" (left padded to 10)
String.Format(“->{1,-10}<-”, “Hello”); // gives "->Hello <-" (right padded to 10)
Run Code Online (Sandbox Code Playgroud) 有没有什么办法让printf一个size_t没有任何第一投射,或产生编译器警告?(我总是编译-Wall.)
我使用以下C++语法在Windows平台上输出浮点值:
printf("%.2f", 1.5);
Run Code Online (Sandbox Code Playgroud)
如果我在英文用户帐户上运行它,它的效果很好.我的假设是,如果我运行它,比如法国用户帐户,输出将是1,50而不是1.50.
为什么我看不到它以及如何产生我想要的结果?
在IRC的谈话中,有人指出以下内容:
decimal.Parse("1.0000").ToString() // 1.0000
decimal.Parse("1.00").ToString() // 1.00
Run Code Online (Sandbox Code Playgroud)
该decimal类型如何/为什么保留这样的精度(或者更确切地说,重要数字)?我的印象是这两个值是相等的,不是很明显.
这也提出了进一步的问题:
我有一些在Python 2.7中运行良好的代码.
Python 2.7.3 (default, Jan 2 2013, 13:56:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from sys import stdout
>>> foo = 'Bar'
>>> numb = 10
>>> stdout.write('{} {}\n'.format(numb, foo))
10 Bar
>>>
Run Code Online (Sandbox Code Playgroud)
但是在2.6中我得到了一个ValueError异常.
Python 2.6.8 (unknown, Jan 26 2013, 14:35:25)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from sys import stdout
>>> foo = 'Bar'
>>> numb = 10
>>> stdout.write('{} {}\n'.format(numb, foo)) …Run Code Online (Sandbox Code Playgroud) python string-formatting backwards-compatibility python-2.6 python-2.7
当尝试使用数组作为string.Format()方法的参数时,我收到以下错误:
FormatException:Index(从零开始)必须大于或等于零且小于参数列表的大小.
代码如下:
place = new int[] { 1, 2, 3, 4};
infoText.text = string.Format("Player1: {0} \n Player2: {1} \n Player3: {2} \n Player4: {3}", place);
Run Code Online (Sandbox Code Playgroud)
Array包含四个值,其中的参数String.Format()也相同.
是什么导致这个错误?
(这infoText.text只是一个常规的String对象)
填充字符串的最简单方法是将左侧的0填充为0
"110"="00000110"
"11110000"="11110000"
我曾尝试使用format!宏,但它只用空间填充到右边:
format!("{:08}", string);
Run Code Online (Sandbox Code Playgroud)