相当于Java的printf("%* F"。) [英] java equivalent to printf("%*.*f")

查看:241
本文介绍了相当于Java的printf("%* F"。)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C中,printf()的语句允许在参数列表中提供的precision长度。

In C, the printf() statement allows the precision lengths to be supplied in the parameter list.

printf("%*.*f", 7, 3, floatValue);

其中星号被替换为第一和第二值分别

where the asterisks are replaced with first and second values, respectively.

我要寻找一个在Android的/ Java中的等价;的String.Format()抛出一个异常。

I am looking for an equivalent in Android/Java; String.format() throws an exception.

编辑:谢谢,@Tenner;它确实工作。

Thanks, @Tenner; it indeed works.

推荐答案

我用

int places = 7;
int decimals = 3;

String.format("%" + places + "." + decimals + "f", floatValue);

一个有点丑陋的(和字符串连接使得它表现不好),但它的工作原理。

A little ugly (and string concatenation makes it not perform well), but it works.

这篇关于相当于Java的printf("%* F"。)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆