如何只打印长号的前4位数 [英] how to print only first 4 digit of long number

查看:79
本文介绍了如何只打印长号的前4位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像阶乘数12的值是479001600,但在控制台窗口我只想打印前4位而不是整数,例如从479001600到4790只能打印前4位数?

解决方案

最简单的方法是使用Substring,但是在使用它之前你需要确保你至少有四位数。





其中一个更难的方法是:



 int x = 479001600; 
int y =(int)System.Math.Log10(x);
int z =(int)(x / System.Math.Pow(10,(y - 3)));


你应该只是能够将int / decimal转换为字符串并随后使用子字符串函数,即



  int  i =  12 ; 
long factorial = fact(i); // 假设这给出479001600

string FirstFourDigits = factorial.ToString();

if (FirstFourDigits.Lenght > = 4
{
Console.WriteLine(FirstFourDigits.Substring( 0 4 ));
}
else
{
Console.WriteLine(FirstFourDigits);
}





这应该是它



问候

Pawan


  int  t =  554344589 ; 
Response.Write(Convert.ToString(t).Substring(Convert.ToString(t).Length - 4 )。PadRight(Convert.ToString(t ).Length,' '));


like the value of factorial number 12 is 479001600 but on console window i want to print only first 4 digit not whole number,like from 479001600 to 4790 only.how can i print first 4 digits?

解决方案

The simplest way is with Substring, but you need to be sure you have at least four digits before you use it.


One of the harder ways is:

int x = 479001600 ;
int y = (int) System.Math.Log10 ( x ) ;
int z = (int) ( x / System.Math.Pow ( 10 , ( y - 3 ) ) ) ;


You should simply be able to convert the int/decimal to a string and subsequently use sub-string function i.e.,

int i = 12;
long factorial = fact(i); //assuming this gives 479001600 

string FirstFourDigits = factorial.ToString(); 

if(FirstFourDigits.Lenght >= 4)
{
  Console.WriteLine(FirstFourDigits.Substring(0, 4));
}
else
{
  Console.WriteLine(FirstFourDigits);
}



And this should be it

Regards
Pawan


int t = 554344589;
            Response.Write(Convert.ToString(t).Substring(Convert.ToString(t).Length - 4).PadRight(Convert.ToString(t).Length, ' '));


这篇关于如何只打印长号的前4位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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