对字符串加倍,保持尾随零 [英] Double to String keeping trailing zero

查看:51
本文介绍了对字符串加倍,保持尾随零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将double值转换为字符串,并使用 Replace()方法
将','替换为'。'。 / p>

这很好用,但仅当尾随数字不为零时,即使值是1234.0,我的字符串也需要为零。这对于十进制值效果很好。我尝试将双精度型转换为十进制,但是如果有零,我将丢失十进制数字。



我知道我丢失了一些内容。我将不胜感激一些建议。

解决方案

这取决于语言。 C#中的一个示例

  d.ToString( 0.00); 

将产生一个带两位小数的双精度值(零或其他)。


I've tried converting the double value into a string and using the Replace() method to replace the ',' to '.'.

This works well but only when the trailing digits are not zero, I need zeros in my string, even if the value is 1234.0. This worked well for the decimal values. I have tried to convert the double to decimal but I lose the decimal digits if there are zeros.

I know I'm missing something. I would be grateful for some suggestions.

解决方案

This would depend on the language. An example in C#

d.ToString("0.00");

Would produce a double with 2 decimal places nomatter the values (zero or otherwise).

这篇关于对字符串加倍,保持尾随零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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