如何将18位数字分成几部分 [英] How to break an 18 digit number into parts

查看:125
本文介绍了如何将18位数字分成几部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将18位数字分成几部分并且不知道如何去做。



例如

num = 123456789123654258 // 18位数字



num由5个联合部分组成,即



braCode = 123, //第1位3位

cusCode = 4567891,//从第4位到第10位

curCode = 23,//第11位和第12位

ledcode = 6542,//第13到第16位

subCode = 58,// 17和18。



我如何获得bracode,来自num的cuscode,curcode,ledcode和子代码。



我尝试过:



i可以得到以下

bracode = num.Substring(0,3)

subCode = num.Substring(16);

curcode =?

ledcode =?

cuscode =?

i will like to break a 18 digit number into parts and dont know how to go about it.

for example
num=123456789123654258 //18 digit number

num consist of 5 joint parts namely

braCode=123, //1st 3 digit
cusCode=4567891, //from 4th to 10th digit
curCode=23, //11th and 12th digit
ledcode=6542, //13th to 16th digit
subCode=58, // 17th and 18th.

how do i get bracode,cuscode,curcode,ledcode and subcode from num.

What I have tried:

i can get the following
bracode = num.Substring(0,3)
subCode= num.Substring(16);
curcode= ?
ledcode= ?
cuscode= ?

推荐答案

long num = 123456789123654258;

int subcode = num%100;

num = Math.Floor(num / 100);

int ledcode = num%10000;

n um = Math.Floor(num / 10000);

int curCode = num%100;

num = Math.Floor(num / 100);

int cusCode = num%10000000;

int braCode = Math.Floor(num / 10000000)
long num=123456789123654258;
int subcode = num%100;
num = Math.Floor(num/100);
int ledcode = num%10000;
num = Math.Floor(num/10000);
int curCode = num%100;
num = Math.Floor(num/100);
int cusCode = num%10000000;
int braCode = Math.Floor(num/10000000)


第一个问题是:你给的是18位数字作为 类型,或作为 字符串

如果 ,那么Nipurba Konar的解决方案3就在正确的轨道上,但它可以简化:

The first question is: "Are you given the 18 digit number as a type long, or as a string?"
If as long, then Solution 3 by Nipurba Konar is on the right track but it can be simplified:
long subCode = Math.DivRem(num, 100, out num);
long ledcode = Math.DivRem(num, 10000, out num);
long curCode = Math.DivRem(num, 100, out num);
long cusCode = Math.DivRem(num, 10000000, out num);
long braCode = num;



如果您有 string ,然后正则表达式非常直接:


If you have a string, then a Regex is pretty straight forward:

class Program
{
  static readonly Regex TakeApart = new Regex("^(...)(.......)(..)(....)(..)


,RegexOptions.Compiled );
静态 void Main( string [] args)
{
string num = 123456789123654258\" ;
匹配m = TakeApart.Match(num);
if (!m.Success)
{
// 出错了!
}
string braCode = m.Groups [ 1 ]值。 // 前3位
string cusCode = m.Groups [ 2 ]。值; // 从第4位到第10位
string curCode = m.Groups [ 3 ]。值; // 第11位和第12位
string ledcode = m.Groups [ 4 ]。价值; // 第13位至第16位
string subCode = m.Groups [ 5 ]。值; // 17日和18日。
// 继续....
}
}
", RegexOptions.Compiled); static void Main(string[] args) { string num = "123456789123654258"; Match m = TakeApart.Match(num); if (!m.Success) { // something went wrong! } string braCode = m.Groups[1].Value; //1st 3 digits string cusCode = m.Groups[2].Value; //from 4th to 10th digit string curCode = m.Groups[3].Value; //11th and 12th digit string ledcode = m.Groups[4].Value; //13th to 16th digit string subCode = m.Groups[5].Value; // 17th and 18th. // proceed .... } }



这给出了5个字符串作为输出。


This gives 5 strings as output.


这篇关于如何将18位数字分成几部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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