有一个十进制动态值,我想动态显示十进制值的最后两位,例如0.12289和1.23564875,要显示0.89和1.75 [英] having a decimal dynamic value which dynamically i want to display the last 2 digits of the decimal value such as 0.12289 and 1.23564875 want to display 0.89 and 1.75

查看:72
本文介绍了有一个十进制动态值,我想动态显示十进制值的最后两位,例如0.12289和1.23564875,要显示0.89和1.75的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

有一个十进制的动态值,我想动态显示十进制值的最后两位,例如0.12289和1.23564875等.想要显示0.89和1.75

代码项目成员的寿命长.

在此先感谢.

Dear All,

having a decimal dynamic value, which dynamically i want to display the last 2 digits of the decimal value such as 0.12289 and 1.23564875 and so on. want to display 0.89 and 1.75

Long life for Code Project Members.

Thanks in advance.

推荐答案

使用Regex 的替代方法如下:

An alternative using Regex is as follows:

double num = 1.23564875;
string numString = Regex.Replace(num.ToString("R"),@"(?<=\d*\.)\d*(?=\d{2})","");
Console.WriteLine (numString);

//Output
//1.75


尝试以下操作:

Try this:

string s = "0.12289";
string[] words = s.Split(".");
string result=words[0]+"."+words[1].Substring (words[1].Length-2);


肮脏,但适用于您的两个测试用例:
Dirty, but working for both of your test cases:
double d = 0.12289;
string ds = d.ToString("R", System.Globalization.CultureInfo.InvariantCulture);
int s = (ds.IndexOf(".") + 1);
int l = ((ds.Length) - s);
string sout = ds.Substring(0, s) + ds.Substring(s+l-2, 2);
MessageBox.Show(sout);


这篇关于有一个十进制动态值,我想动态显示十进制值的最后两位,例如0.12289和1.23564875,要显示0.89和1.75的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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