C# 检查整数变量的长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10845820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:30:03  来源:igfitidea点击:

Check the length of integer variable

c#

提问by CrBruno

Is there a way to check a lenth of integer variable, and if is to long just trim it. I hava a field in database that accepts 3 character, lenth is 3.

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

So is it possible to do like it's done with string variable

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

example:

例子:

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

Thanks!

谢谢!

采纳答案by Cliff

The following worked a treat for me!

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

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

Original Source: http://www.java2s.com/Code/CSharp/Data-Types/Getthedigitlengthofanintvalue.htm

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

回答by Romil Kumar Jain

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

or simply use

或者干脆使用

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

回答by Raab

Use like this

像这样使用

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

回答by Tigran

Nont very clear what you're asking for, but as much as I unerstoodyou're asking for:

不是很清楚你在要求什么,但正如我所了解的那样,你要求的是:

int a = 1234567890; 

for some reason you want to make it shorter, like

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

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

If so, the easiest solution may be, convert it to string, made what you already implemented and reconvert it back to int.

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

If this is not what you're asking for, please clarify.

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

回答by Md Kamruzzaman Sarker

Checking length

检查长度

     length = cust_ref.ToString().Length;

Remove extra bits

删除额外的位

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

回答by Ice Box

for this u will have to do some simple stuffs. like

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

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

or u can manually store it in any variable and the

或者你可以手动将它存储在任何变量中

回答by Guffa

You don't have to convert it to a string to make it shorter, that can be done numerically:

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

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

This will cut of digits from the left, if you want to cut them off from the right:

如果你想从右边切掉它们,这将从左边切掉数字:

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

If the value can be negative, also check:

如果该值可以是负数,还要检查:

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

or:

或者:

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

回答by Viacheslav Smityukh

The conversion to the string is ugly way to implement it.

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

It's require a pure math solution

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

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

  return number;
}

回答by Cliff

The easiest answer would be:

最简单的答案是:

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

回答by Snehal Thakkar

You can try this code. use if else statement to check the validation ..

你可以试试这个代码。使用 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();
        }
    }
}