如果第一个小数点后的第一个值为0,如何忽略该值;如果大于0,如何忽略它? [英] How to ignore value after first decimal point first value if it is 0 else take it if greater than 0?

查看:61
本文介绍了如果第一个小数点后的第一个值为0,如何忽略该值;如果大于0,如何忽略它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的值如下:

decimal val = 1.100;

现在我想做的是,如果我在第一个小数点后的值是0,那么我只想取1.1而忽略休息,而如果我是1.11,那么我想取整个1.11

Now what i am trying to do is if i am having 0 after first decimal point value then i want to take only 1.1 and ignore rest and if i am having 1.11 then i want to take whole 1.11

这就是我的想法:

1)将小数转换为字符串.

1) Convert decimal to string.

2)按点分隔字符串.

2) Split string by dot.

3)检查长度是否为2,并且如果第二个位置大于0,则将十进制值1.11取整为11,否则取整,如十进制为1.10则取整,否则取1.1,否则取1.0,如果十进制为1.000

3) Check if length is 2 and if second position is greater than 0 then take whole like 11 for decimal value 1.11 else if decimal is like this 1.10 then take 1.1 else take 1.0 in case of decimal 1.000

样本输入和输出:

1.000 // output 1.0
1.100 // output 1.1
1.110 // output 1.11
1.111 // output 1.111
1.102 // output 1.102

但是这是我要避免的肮脏逻辑.是否有任何内置方法或更好的方法可以做到这一点?

But this is dirty logic which i want to avoid.Isnt there is any inbuilt way or better way to do this??

注意::我不想进行任何形式的汇总.

Note: I dont want to do any kind of round up.

推荐答案

您可以使用

这篇关于如果第一个小数点后的第一个值为0,如何忽略该值;如果大于0,如何忽略它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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