Click here to Skip to main content
15,880,364 members
Articles / Programming Languages / C#
Tip/Trick

Get First N Digits of a Number

Rate me:
Please Sign up or sign in to vote.
4.34/5 (28 votes)
3 May 2016CPOL 62.4K   13   50
This is about how to get the first N digits of a number without converting it to string

Introduction

This snippet returns the first N digits of a number without converting the number to string.

Background

I was asked by my senior colleague to make a method which takes two arguments as input, first the number and second the number of digits to be returned from the first argument number, but the restriction was not to convert it to string.

Using the Code

I came up with this method which worked pretty well for me:

/// <summary>
/// Returns first part of number.
/// </summary>
/// <param name="number">Initial number</param>
/// <param name="N">Amount of digits required</param>
/// <returns>First part of number</returns>
private static int takeNDigits(int number, int N)  
{  
     // this is for handling negative numbers, we are only insterested in postitve number
     number =Math.Abs(number);

     // special case for 0 as Log of 0 would be infinity
     if(number == 0)
        return number;

    // getting number of digits on this input number
    int numberOfDigits = (int)Math.Floor(Math.Log10(number) + 1);  
    
    // check if input number has more digits than the required get first N digits
    if (numberOfDigits >= N)  
        return (int)Math.Truncate((number / Math.Pow(10, numberOfDigits - N)));  
    else  
        return number;
}  

I tested with some inputs which are:

C#
int Result1 = takeNDigits(666, 4);  
int Result2 = takeNDigits(987654321, 5);  
int Result3 = takeNDigits(123456789, 7);  
int Result4 = takeNDigits(35445, 1);  
int Result5 = takeNDigits(666555, 6);  

The output was:

Result1: 666
Result2: 98765
Result3: 1234567
Result4: 3
Result5: 666555

Points of Interest

The thing that took me some time was to figure out how to determine the number of digits the input number has, but after some effort and research, I was able to get to it.

History

  • 04/28/2016: Initial version
  • 04/30/2016: Documentation addition as suggested in comments
  • 05/03/2016 Handled case of 0 and negative numbers

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Pakistan Pakistan
Ehsan Sajjad is a Microsoft Certified Professional, Microsoft Certified C# Specialist and he is also among the top users on StackOverflow from Pakistan with 50k+ reputation at time of writing this and counting.

He is a passionate software developer with around 5 years of professional experience in Microsoft Technologies both web and desktop applications and always open to learn new things and platforms especially in mobile application development and game development.

Some Achievements :

  • 5th Top Contributor from Pakistan on Stackoverflow.com
  • Top User in ASP.NET MVC from Pakistan on Stackoverflow.com
  • 21st June 2017 - Article of the Day - ASP.NET Community (Beginners Guide on AJAX CRUD Operations in Grid using JQuery DataTables in ASP.NET MVC 5)
  • 19th April 2017 - Article of the Day - ASP.NET Community (ASP.NET MVC Async File Uploading using JQuery)
  • March 2017 - Visual C# Technical Guru Silver Medal on Microsoft Tech Net Wiki Article Competition
  • 20 January 2017 - Article of the Day - ASP.NET Community (Async File Uploading in ASP.NET MVC)
  • 22nd September 2016 - Article of the Day - ASP.NET Community (GridView with Server Side Filtering, Sorting and Paging in ASP.NET MVC 5)
  • 22nd August 2016 - Article of the Day - ASP.NET Community (Beginners Guide for Creating GridView in ASP.NET MVC 5)
  • December 2015 - C-SharpCorner Monthly Winner

Comments and Discussions

 
AnswerRe: 3, But Why?? Pin
Dmitriy Gakh28-Apr-16 8:02
professionalDmitriy Gakh28-Apr-16 8:02 
GeneralRe: 3, But Why?? Pin
Kevin Marois28-Apr-16 8:25
professionalKevin Marois28-Apr-16 8:25 
GeneralRe: 3, But Why?? Pin
Ehsan Sajjad28-Apr-16 8:33
professionalEhsan Sajjad28-Apr-16 8:33 
GeneralRe: 3, But Why?? Pin
ArchAngel12329-Apr-16 7:54
ArchAngel12329-Apr-16 7:54 
GeneralRe: 3, But Why?? Pin
Ehsan Sajjad29-Apr-16 9:12
professionalEhsan Sajjad29-Apr-16 9:12 
GeneralRe: 3, But Why?? Pin
ArchAngel12329-Apr-16 9:28
ArchAngel12329-Apr-16 9:28 
GeneralRe: 3, But Why?? Pin
Ehsan Sajjad29-Apr-16 9:46
professionalEhsan Sajjad29-Apr-16 9:46 
GeneralRe: 3, But Why?? Pin
Dmitriy Gakh29-Apr-16 17:30
professionalDmitriy Gakh29-Apr-16 17:30 
A good example for my students Smile | :) . Almost no real world use, but many theoretical outcomes Smile | :) . Exercise for the brain Smile | :) .
GeneralRe: 3, But Why?? Pin
Nadeem Jamali30-Apr-16 8:20
Nadeem Jamali30-Apr-16 8:20 
AnswerRe: 3, But Why?? Pin
Ehsan Sajjad11-Jan-18 7:12
professionalEhsan Sajjad11-Jan-18 7:12 
GeneralMy vote of 5 Pin
raddevus28-Apr-16 4:59
mvaraddevus28-Apr-16 4:59 
GeneralRe: My vote of 5 Pin
Ehsan Sajjad28-Apr-16 7:58
professionalEhsan Sajjad28-Apr-16 7:58 
QuestionClever Trick Pin
2Programmer7828-Apr-16 4:46
2Programmer7828-Apr-16 4:46 
QuestionAny Performance Gain? Pin
wmjordan28-Apr-16 4:43
professionalwmjordan28-Apr-16 4:43 
AnswerRe: Any Performance Gain? Pin
Emily Heiner28-Apr-16 5:42
Emily Heiner28-Apr-16 5:42 
GeneralRe: Any Performance Gain? Pin
Dominik Reichl28-Apr-16 8:41
Dominik Reichl28-Apr-16 8:41 
GeneralRe: Any Performance Gain? Pin
Ehsan Sajjad28-Apr-16 10:17
professionalEhsan Sajjad28-Apr-16 10:17 
GeneralRe: Any Performance Gain? Pin
ArchAngel12329-Apr-16 7:56
ArchAngel12329-Apr-16 7:56 
GeneralRe: Any Performance Gain? Pin
Ehsan Sajjad29-Apr-16 9:13
professionalEhsan Sajjad29-Apr-16 9:13 
GeneralRe: Any Performance Gain? Pin
Dominik Reichl28-Apr-16 10:17
Dominik Reichl28-Apr-16 10:17 
AnswerRe: Any Performance Gain? Pin
Dmitriy Gakh28-Apr-16 7:22
professionalDmitriy Gakh28-Apr-16 7:22 
GeneralRe: Any Performance Gain? Pin
Dmitriy Gakh29-Apr-16 5:47
professionalDmitriy Gakh29-Apr-16 5:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.