%b,%c,%d,%f,%s 这在java中如何工作?我一直在尝试阅读Formatter类和formattable接口,但是我无法理解作为参数传递的转换。
例如:
System.out.printf("%f not equals %b", Math.PI, Math.E)
即使%b,%c,%d,%f,%s等格式化程序在ocjp6考试中受到限制,感觉就像准备的话题一样
答案 0 :(得分:1)
我认为您无法理解System.out.printf()的工作原理。一旦你明白这一点,这很简单。
您的原始问题是关于以下
System.out.printf("%f not equals %b", Math.PI, Math.E)
这里System.out.printf正在尝试输出一个String。 %f和%b可以理解为具有特殊含义的占位符。
占位符因为它们将被逗号后面的数据替换。在这种情况下,%f被替换为Math.PI的值,%b被替换为Math.E的值
特殊含义,因为每个格式化程序代表某些事情,例如如上所述
%b is for Boolean
%f is for Decimal floating-point
%c is for Character
%d is for Decimal integer
%s is for String
现在以简单的方式编写原始查询
System.out.printf("%f is not equals to %b", 3.14, true);
此处%f(表示十进制浮点数)替换为浮点值3.14,%b替换为值“true”。
如果你在上面转换%f和%b,如
System.out.printf("%b is not equal to %f", 3.14, true); //error
because "true" (boolean)value is not compatible with %f
但这会起作用
System.out.printf("%b is not equal to %f", 3.14, 3.14);
//将起作用,因为3.14将评估为true
上面的工作是因为java的一些自动类型转换。但你可以稍后再研究一下。
现在关于你的上一个问题
中发生了什么System.out.println("%+04.2f",12.6542); ?
我猜你的意思是printf。
现在所有格式化程序及其说明都出现在链接
中http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html
这可能看起来令人生畏。但这很容易。
Lets figure out what %+04.2f stands for from the above link
'+' Requires the output to include a positive sign for all positive numbers.
If this flag is not given only negative values will include sign.
'0' Requires the output to be padded with leading zeros to the minimum field width following any sign. Called zero padding
4.2 indicates that a floating point number is displayed in a total of 4 character spaces, including 2 digits after the decimal.
that means 22.5555 will be shown as 22.55(total 4 char and two after space)
Read Width and precision in the link given above.
f floating point The result is formatted as a decimal number
所以基本上%+ 04.2f的含义是对所有正数都显示一个正号。数字总共应该有4个字符,小数后应该有2个字符。它应格式化为浮点数。
更多例子
System.out.printf(" %04.2f",12.6542); output ==> 12.65
System.out.printf(" %+04.2f",12.6542); output ==> +12.65(plus sign here bcoz we gave +)
System.out.printf(" %+04.2f",-12.6542); output ==> -12.65
System.out.printf(" %02d",1); output ==> 1
System.out.printf(" %02d",1); output ==> 01 (bcoz of 02d)
System.out.printf(" %03d",1); output ==> 001 (bcoz of 03d)
System.out.printf(" %+04.2f",22.2); output ==> +22.20
System.out.printf(" %+04.2f",2222.125); output ==> +2222.13
(left side of decimal is never truncated . so all chars shows ie total 6 chars even though only 4 asked
System.out.printf(" %+04.0f",2222.125); output ==> +2222 (bcoz zero chars requested after decimal point)
请仔细阅读以下链接。它将帮助您更轻松地理解概念
http://www.homeandlearn.co.uk/java/java_formatted_strings.html
https://answers.yahoo.com/question/index?qid=20101017181211AAbtWC0
https://docs.oracle.com/javase/tutorial/java/data/numberformat.html
http://alvinalexander.com/programming/printf-format-cheat-sheet
答案 1 :(得分:0)
var ctx = document.getElementById("chart-area").getContext("2d");
var gradient1 = ctx.createLinearGradient(0, 0, 0, 175);
gradient1.addColorStop(0.0, '#ACE1DB');
gradient1.addColorStop(1.0, '#7FBDB9');
var gradient2 = ctx.createLinearGradient(0, 0, 400, 400);
gradient2.addColorStop(0, '#B5D57B');
gradient2.addColorStop(1, '#98AF6E');
var gradient3 = ctx.createLinearGradient(0, 0, 0, 175);
gradient3.addColorStop(0, '#E36392');
gradient3.addColorStop(1, '#FE92BD');
var gradient4 = ctx.createLinearGradient(0, 0, 0, 175);
gradient4.addColorStop(1, '#FAD35E');
gradient4.addColorStop(0, '#F4AD4F');
/* ADD DATA TO THE DOUGHNUT CHART */
var doughnutData = [
{
value: 80,
color: gradient1,
highlight: "#E6E6E6",
label: "NUTRIENTS"
},
{
value: 20,
color:"#E6F1EE"
},
{
value:50,
color: gradient2,
highlight: "#E6E6E6",
label: "PROTEINE"
},
{
value: 50,
color:"#E6F1EE"
},
{
value: 75,
color: gradient3,
highlight: "#E6E6E6",
label: "FETTE"
},
{
value:25,
color:"#E6F1EE"
},
{
value: 77,
color: gradient4,
highlight: "#E6E6E6",
label: "CARBS"
}
{
value: 23,
color:"#E6F1EE"
},
];
您可以查看reference。 Oracle docs也详细解释了这一点。