7

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

那么是否可以像使用字符串变量那样做

例子:

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

谢谢!

4

10 回答 10

31

最简单的答案是:

//The length would be 3 chars.    
public int myint = 111;
myint.ToString().Length;
于 2014-12-18T12:56:28.373 回答
15

以下对我来说是一种享受!

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;
}

原始来源:http ://www.java2s.com/Code/CSharp/Data-Types/Getthedigitlengthofanintvalue.htm

于 2014-04-10T21:12:22.123 回答
4

您不必将其转换为字符串以使其更短,这可以通过数字方式完成:

if (num > 999) {
  num %= 1000;
}

如果您想从右侧截断数字,这将从左侧截断数字:

while (num > 999) {
  num /= 10;
}

如果该值可以为负,请检查:

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

或者:

while (num < -99) {
  num = -(-num / 10);
}
于 2012-06-01T07:14:46.693 回答
1
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));
于 2012-06-01T07:04:17.080 回答
1

像这样使用

 cust_ref=   cust_ref.Tostring().Length > 20 ? Convert.ToInt32(cust_ref.ToString().Substring(0, 19)) : cust_ref; 
于 2012-06-01T07:04:31.530 回答
1

不是很清楚你要什么,但据我所知,你要的是:

int a = 1234567890; 

出于某种原因,你想让它更短,比如

 int b = MakeShorter(a); 
    //b == 1234 (say)

如果是这样,最简单的解决方案可能是将其转换为字符串,制作您已经实现的内容并将其重新转换回 int。

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

于 2012-06-01T07:06:11.963 回答
1

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

它需要一个纯数学解决方案

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

  return number;
}
于 2012-06-01T07:32:07.473 回答
0

检查长度

     length = cust_ref.ToString().Length;

删除多余的位

       if (length > need)
       {
           cust_ref =Convert.ToInt32( cust_ref.ToString().Remove(length -(length- need)));
       }
于 2012-06-01T07:08:39.590 回答
0

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

cust_ref = Convert.ToInt32(Convert.ToString(cust_ref).Substring(0, 19));

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

于 2012-06-01T07:13:13.687 回答
0

你可以试试这段代码。使用 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();
        }
    }
}
于 2016-02-21T01:25:19.917 回答