C# 检查整型变量的长度

C# 检查整型变量的长度,c#,C#,有没有办法检查整型变量的长度,如果是长的,就修剪它。 我在数据库中有一个接受3个字符的字段,长度是3 那么,有没有可能像使用字符串变量一样进行操作 例如: cust_ref = cust_ref.Length > 20 ? cust_ref.Substring(0, 19) : cust_ref; 谢谢 cust_ref = cust_ref.ToString().Length > 20 ? Convert.ToInt32(cust_ref.ToString().Substrin

有没有办法检查整型变量的长度,如果是长的,就修剪它。 我在数据库中有一个接受3个字符的字段,长度是3

那么,有没有可能像使用字符串变量一样进行操作

例如:

cust_ref = cust_ref.Length > 20 ? cust_ref.Substring(0, 19) : cust_ref; 
谢谢

cust_ref = cust_ref.ToString().Length > 20 ? Convert.ToInt32(cust_ref.ToString().Substring(0, 19)) : cust_ref; 
或者干脆用

cust_ref = Convert.ToInt32(Convert.ToString(cust_ref).Substring(0, 19));
像这样使用

 cust_ref=   cust_ref.Tostring().Length > 20 ? Convert.ToInt32(cust_ref.ToString().Substring(0, 19)) : cust_ref; 

你的要求不是很清楚,但我知道你的要求是:

int a = 1234567890; 
出于某种原因,你想把它缩短,比如

 int b = MakeShorter(a); 
    //b == 1234 (say)
如果是这样,最简单的解决方案可能是,将其转换为字符串,生成您已经实现的内容,并将其重新转换回int

如果这不是您想要的,请澄清。

检查长度

     length = cust_ref.ToString().Length;
删除多余的位

       if (length > need)
       {
           cust_ref =Convert.ToInt32( cust_ref.ToString().Remove(length -(length- need)));
       }

为此,你将不得不做一些简单的事情。 像


或者,您可以手动将其存储在任何变量中,

您不必将其转换为字符串以使其变短,这可以通过数字实现:

if (num > 999) {
  num %= 1000;
}
如果要从右侧切断数字,这将从左侧切断数字:

while (num > 999) {
  num /= 10;
}
如果该值可能为负值,还应检查:

if (num < -99) {
  num = -(-num % 100);
}
if(num<-99){
num=-(-num%100);
}
或:

while(num<-99){
num=-(-num/10);
}

转换为字符串是实现它的一种丑陋的方式

这需要一个纯数学解

int CutTheNumber(int number, int maxLen)
{
  var maxSize = (int)Math.Pow(10, maxlen);
  if(maxSize <= Math.Abs(number))
  {
    number %= maxSize;
  }

  return number;
}
int-CutTheNumber(int-number,int-maxLen)
{
var maxSize=(int)Math.Pow(10,maxlen);

如果(maxSize以下内容对我有用

public static int IntLength(int i)
{
  if (i < 0)
    throw new ArgumentOutOfRangeException();
  if (i == 0)
    return 1;
  return (int)Math.Floor(Math.Log10(i)) + 1;
}
publicstaticintlength(inti)
{
if(i<0)
抛出新ArgumentOutOfRangeException();
如果(i==0)
返回1;
返回(内部)数学楼层(数学日志10(i))+1;
}

原始资料来源:

最简单的答案是:

//The length would be 3 chars.    
public int myint = 111;
myint.ToString().Length;

您可以尝试此代码。使用if-else语句检查验证

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace avaragescore
{
    class Program
    {

        static void Main(string[] args)
        {
            float quiz;
            float midterm;
            float final;
            float avrg=0;
        Start:
            Console.WriteLine("Please enter the Quize Score here");
            quiz = float.Parse(Console.ReadLine());
            if(quiz > 100)
            {
                Console.WriteLine("You have entered wrong score please re-enter");
                goto Start;
            }
            Start1:
            Console.WriteLine("Please enter the Midterm Score here");
            midterm = float.Parse(Console.ReadLine());
            if(midterm > 100)
            {
                Console.WriteLine("You have entered wrong score please re- enter");
                goto Start1;
            }
            Start3:
            Console.WriteLine("Please enter the Final Score here");
            final = float.Parse(Console.ReadLine());
            if(final > 100)
            {
                Console.WriteLine("You have entered wrong Score Please re-enter");
                goto Start3;
            }
            avrg = (quiz + midterm + final) / 3;

            if(avrg >= 90)
            {
                Console.WriteLine("Your Score is {0} , You got A grade",avrg);
            }
            else if (avrg >= 70 && avrg < 90)
            {
                Console.WriteLine("Your Score is {0} , You got B grade", avrg);
            }
            else if (avrg >= 50 && avrg < 70)
            {
                Console.WriteLine("Your Score is {0} , You got C grade", avrg);
            }
            else if (avrg < 50)
            {
                Console.WriteLine("Yor Score is {0} , You are fail", avrg);
            }
            else
            {
                Console.WriteLine("You enter invalid Score");
            }
            Console.ReadLine();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间avaragescore
{
班级计划
{
静态void Main(字符串[]参数)
{
浮动测验;
中期浮动;
浮动决赛;
浮动avrg=0;
开始:
Console.WriteLine(“请在此输入测验分数”);
quick=float.Parse(Console.ReadLine());
如果(测验>100)
{
Console.WriteLine(“您输入了错误的分数,请重新输入”);
转到开始;
}
Start1:
Console.WriteLine(“请在此输入期中成绩”);
midterm=float.Parse(Console.ReadLine());
如果(期中>100)
{
WriteLine(“您输入了错误的分数,请重新输入”);
后藤星1;
}
Start3:
Console.WriteLine(“请在此输入最终分数”);
final=float.Parse(Console.ReadLine());
如果(最终>100)
{
Console.WriteLine(“您输入了错误的分数,请重新输入”);
后藤星3;
}
avrg=(测验+期中+期末)/3;
如果(avrg>=90)
{
WriteLine(“你的分数是{0},你得到了分数”,avrg);
}
否则如果(avrg>=70&&avrg<90)
{
WriteLine(“你的分数是{0},你得了B级”,avrg);
}
否则如果(avrg>=50&&avrg<70)
{
WriteLine(“你的分数是{0},你得了C分”,avrg);
}
否则如果(avrg<50)
{
WriteLine(“你的分数是{0},你失败了”,avrg);
}
其他的
{
Console.WriteLine(“您输入的分数无效”);
}
Console.ReadLine();
}
}
}

好的,我知道了,关键是将变量转换为字符串..谢谢!第二个版本只有在值始终至少为19个字符时才有效,否则您会得到
ArgumentOutOfRangeException
.int->string->string->int:(如果变量太长,我必须对其进行修剪,因此首先我必须将其转换为字符串,然后再转换为int?@CrBruno:我认为这是一个最简单的解决方案。如果执行此代码,将创建3个额外的字符串实例否,它不需要数值解。对不起,我不明白您的意思:(你说它需要一个纯数学的解决方案,但那不是真的。其他解决方案也可以,这只是一个品味问题。你是对的,所有的解决方案都可以,但都不能很好地工作!字符串的处理成本太高,在你写CD-Rejector之前,它对很多应用程序都很关键。然后你应该删除昂贵的浮动点。)从代码中执行t操作。这将使其速度提高大约十倍。如果数据库中有一个存储整数的字段,为什么它是字符类型字段而不是整数?这里的惰性答案是“4字节;
int
始终是4字节”.当使用代码回答问题时,解释代码在做什么以及它为什么回答提问者的问题是一种很好的形式。你能在你的答案中添加一些吗?
var result=(int)Math.Floor(Math.Log10(Math.Abs(cust_ref))+(cust_ref>0?1d:2d))
//The length would be 3 chars.    
public int myint = 111;
myint.ToString().Length;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace avaragescore
{
    class Program
    {

        static void Main(string[] args)
        {
            float quiz;
            float midterm;
            float final;
            float avrg=0;
        Start:
            Console.WriteLine("Please enter the Quize Score here");
            quiz = float.Parse(Console.ReadLine());
            if(quiz > 100)
            {
                Console.WriteLine("You have entered wrong score please re-enter");
                goto Start;
            }
            Start1:
            Console.WriteLine("Please enter the Midterm Score here");
            midterm = float.Parse(Console.ReadLine());
            if(midterm > 100)
            {
                Console.WriteLine("You have entered wrong score please re- enter");
                goto Start1;
            }
            Start3:
            Console.WriteLine("Please enter the Final Score here");
            final = float.Parse(Console.ReadLine());
            if(final > 100)
            {
                Console.WriteLine("You have entered wrong Score Please re-enter");
                goto Start3;
            }
            avrg = (quiz + midterm + final) / 3;

            if(avrg >= 90)
            {
                Console.WriteLine("Your Score is {0} , You got A grade",avrg);
            }
            else if (avrg >= 70 && avrg < 90)
            {
                Console.WriteLine("Your Score is {0} , You got B grade", avrg);
            }
            else if (avrg >= 50 && avrg < 70)
            {
                Console.WriteLine("Your Score is {0} , You got C grade", avrg);
            }
            else if (avrg < 50)
            {
                Console.WriteLine("Yor Score is {0} , You are fail", avrg);
            }
            else
            {
                Console.WriteLine("You enter invalid Score");
            }
            Console.ReadLine();
        }
    }
}