修剪和删除字符串中的数字 [英] Trimming and removing digits from strings

查看:86
本文介绍了修剪和删除字符串中的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我有值000000099

并且在一些修剪方法之后,该值最终显示为.99

这里实际上修剪了前面的所有零和从DOT显示(正面全部0被删除或修剪)即;最后它显示为.99



所以我需要,如果值小于1,如0.99或0.73或0.35
它应该显示为0.99或0.73

但不像.99或.73

请帮帮我这个朋友.. ............



我的尝试:



  public   string  FormateStringAmount( string  strValue) //  此处字符串值变为0000000099为0.99值 
{
尝试
{
如果 (strValue.Trim()== return 0.00;

string strTrimString = strValue.Trim();
string strAmount = 0.00;
int intLength = strTrimString.Length;

if (strTrimString.EndsWith( +))
{
strAmount = strTrimString.Substring( 0 ,intLength -3)+ +
strTrimString.Substring(intLength -3, 2 );
}
else if (strTrimString.EndsWith( - ))
{
strAmount = strTrimString.Substring( 0 ,intLength -3)+ +
strTrimString .Substring(intLength -3, 2 )+ - ;
}
else
{
strAmount = strTrimString.Substring( 0 ,intLength -2)+ +
strTrimString.Substring( intLength -2, 2 );
}

strAmount = strAmount.TrimStart(' 0' ); // 这里的值正在调整前零点
if (strAmount.Equals( 。00)|| strAmount .Equals( 。00 - ))
strAmount = 0.00;
return strAmount;
}
Catch(例外)
{
return strValue;
}
}

解决方案

请阅读:

标准数字格式字符串| Microsoft Docs [ ^ ]

自定义数字格式字符串| Microsoft Docs [ ^ ]



获取数值的一种正确方法是使用Double.Parse方法将字符串转换为double,然后显示它通过使用ToString()方法。例如:



  string  stramount =   000000099; 
double dblamount = Double .Parse(stramount,System.Globalization.NumberStyles.AllowDecimalPoint,System .Globalization.CultureInfo.InvariantCulture);
dblamount / = 100 ;
Console.WriteLine(dblamount.ToString( 0.00)); // 显示0.99





祝你好运!


我理解你的要求是这样的:

- 你总是有一个只包含数字的字符串。

- 你总是希望将字符串的最后2个数字作为小数。



所以你的代码看起来像这样:

  string  s =   000000099; 
int i =( int )s;
double d =( double )i / 100 ;
string s2 = d.ToString( 0.00\" );



我已经分步完成了,所以你可以看看它是如何工作的。

代码本身可以被压缩...


Actually i have the value "000000099"
and after some trimming methods the value finally get displayed as ".99"
Here actually trimming the front all zero's and displaying from DOT(front all 0's are removed or trimmed) i.e; finally it displays as ".99"

So here i need, if the value less than 1 like "0.99" or "0.73" or "0.35"
it should get displayed as "0.99 or 0.73"
but not like ".99 or .73"
Please Help me out with this friends...........!!

What I have tried:

public string FormateStringAmount( string strValue ) // here string value is getting like 0000000099 for 0.99 value
{
try
{
if (strValue.Trim () =="" ) return "0.00" ;

string strTrimString = strValue.Trim(); 		
string strAmount  = "0.00";
int intLength =strTrimString.Length ;

if ( strTrimString.EndsWith("+")  )
  {
    strAmount =  strTrimString.Substring(0,intLength -3)+"." + 
    strTrimString.Substring( intLength -3,2) ;
  }
else if (  strTrimString.EndsWith("-"))
  {
    strAmount =  strTrimString.Substring(0,intLength -3)+"." + 
    strTrimString.Substring( intLength -3,2)+"-" ;
  }
else
  {
    strAmount = strTrimString.Substring(0,intLength -2)+"." + 
    strTrimString.Substring( intLength -2,2);
  }

  strAmount = strAmount.TrimStart ('0');// here the value is getting trimmed front Zero's
   if (strAmount.Equals (".00") || strAmount.Equals (".00-"))  
   strAmount ="0.00" ;
   return strAmount;
    }
	Catch (Exception)
	{	 
	return strValue;
	}
}

解决方案

Please, read this:
Standard Numeric Format Strings | Microsoft Docs[^]
Custom Numeric Format Strings | Microsoft Docs[^]

A proper way to get numeric value is to convert string into double by using Double.Parse method, then to display it by using ToString() method. For example:

string stramount = "000000099";
double dblamount = Double.Parse(stramount, System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture);
dblamount /= 100;
Console.WriteLine(dblamount.ToString("0.00")); //displays 0.99



Good luck!


I understand your requirement like this :
- You have allways a string which only contains numbers.
- You allways want to have the last 2 numbers of the string as decimals.

So your code could look like this :

string s = "000000099";
int i = (int)s;
double d = (double)i / 100;
string s2 = d.ToString("0.00");


I have done it in steps so you could see how it works.
The code itself could be compressed ...


这篇关于修剪和删除字符串中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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