在System.out.format中使用NumberFormat导入

时间:2018-10-20 21:29:27

标签: java for-loop netbeans system.out money-format

我正在为一个学校作业编写一个复利计划。我尝试使用System.out.format();并使用money.format格式化变量投资,利息和investedTotal。我不知道为什么,但是它不断向我抛出错误 “格式说明符'%.2f',参数2、3和4的无效值类型'String'”我已经尝试了好一阵子了,但我似乎仍然找不到原因。

-A

template<class T>struct tag_t{using type=T;};
template<class T>constexpr tag_t<T> tag{};
template<class Tag>using type_t=typename Tag::type;

auto f = [](auto tag_T){ return []( std::vector<type_t<decltype(tag_T)>> const& v){ /* code */ }; };

}

1 个答案:

答案 0 :(得分:1)

getCurrencyInstance返回一个字符串,因此无法使用%.2f进行格式化。

您最好看看NumberFormat是如何工作的: working code on repl.it

如您所见,格式化的结果是一个字符串,当您将String.format与%.2f一起使用时,您应该输入数字,例如: System.out.format(“%2s |%.2f \ n”,1.001,1.005);

我不确定您要使用NumberFormat尝试什么,如果您进行分类,我将能够进一步帮助您解决这个问题。