简体   繁体   English

如何在Java中将bigDecimal数据保存到文件

[英]How to save a bigDecimal data to file in Java

I would like to know how to save a bigDecimal data to a file. 我想知道如何将bigDecimal数据保存到文件中。

After searching the stack overflow, I noticed that if you are dealing with financial calculations, you need to use BigDecimal and not double. 搜索堆栈溢出后,我注意到,如果您要处理财务计算,则需要使用BigDecimal而不是double。 To follow up on that, say for example, a Tip Calculator, Is it recommended to use double or BigDecimal? 要对此进行跟进,例如提示计算器,是否建议使用double或BigDecimal?

If we use BigDecimal, how would save the data to a file. 如果使用BigDecimal,如何将数据保存到文件中。

I want to write the BigDecimal data to a file using FileUtils library. 我想使用FileUtils库将BigDecimal数据写入文件。

Thanks for your help! 谢谢你的帮助!

You have 2 choices: Serialize the object and save it that way (Use ObjectOutputStream), or you could convert it to a String (this is what I'd do) and save the serialized string. 您有2个选择:序列化对象并以这种方式保存(使用ObjectOutputStream),或者可以将其转换为String(这是我要做的)并保存序列化的字符串。 BigDecimal has a string constructor, so it is easy to go back and forth (BigDecimal/String). BigDecimal具有字符串构造函数,因此很容易往返(BigDecimal / String)。 ObjectOutputStream seems to be incompatible with FileUtils without a serious workaround, so you might want to do the string conversion method 如果没有认真的解决方法,ObjectOutputStream似乎与FileUtils不兼容,因此您可能需要执行字符串转换方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM