Java中如何控制右对齐输出?
2个回答
展开全部
%4.xx 使输出右对齐,不足4位用空格;
%-10s 使字符串左对齐输出,不足10位,后加空格;
%10s 使字符串右对齐输出,不足10位,前加空格。
输出的对齐格式:
printf("%4.d\n",1);
printf("%4.d\n%4.d\n%4.d\n",3,12,1123);
printf("%-10s%d\n",s,12);
%4.xx 使输出右对齐;不足4位用空格;
%.4xx 使小数保留4位小数,若该数为整形,不足四位前填0;多余4位原样输出;
%-4.xx 使输出的数左对齐;
%-10s 使字符串左对齐输出,不足10位,后加空格;
%10s 使字符串右对齐输出,不足10位,前加空格;
%10.s 不输出字符,只输出10位空格;
%.10s 字符原样输出;
举例:
#include<stdio.h>
int main()
{
printf("%4.d%4.d%4.d\n",1,34,12344);
printf("%4.d\n%4.d\n%4.d\n",3,12,11123); //右对齐;不足四位,前加空格;
printf("%.4d\n%.4d\n%.4f\n%.4f\n",23,2342,2.3,343.32); //对小数,保留四位小数;对整数,不足四位,前加零;
printf("%-4.d%-4d\n",1,23423); //左对齐;不足四位,后加空格;
printf("%-10s%-10s\n","asdsd","asdsfdgfhgh"); //不足十位,后加空格;
printf("%10s%10s\n","sdsfsf","sdsfdsgsgf"); //不足十位,前加空格;
printf("%10.sq\n","asdaff"); //输出十个空格;
printf("%.10s%.10s\n","asdad","sdsfdsgfgg"); //字符串原样输出;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询