Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Need to get last digit of number - what's most efficient way?

Posted on 2014-01-02
6
399 Views
Last Modified: 2014-01-02
I have a value:

$overall = "598397";  //for example

I need to get the last digit of that.

I've done a bit of research and have found two solutions commonly bantered about:

$last_digit = $overall % 10;

and

$last_digit = substr($overall, -1);

Which is the most efficient way to do this?

$overall will always be positive, will be '0' sometimes, and can grow quite large (low millions possible),

Thanks for any thoughts on this,

Chris
0
Comment
Question by:St_Aug_Beach_Bum
  • 3
  • 2
6 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39751678
I'd probably use either of those.  I think the results will usually be the same.
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39751689
Or maybe not.  It will depend on the input you have.  

See http://www.laprbass.com/RAY_temp_staug.php

<?php // RAY_temp_staug.php
error_reporting(E_ALL);

$nums = array
( 1
, 123
, 0
, 42
, 3.1415926
, 1000000009
, "1,007"
)
;
foreach ($nums as $num)
{
    $mod = $num % 10;
    $sub = substr($num, -1);
    echo PHP_EOL . "<br>INPUT NUMBER: $num, MOD: $mod, SUBSTR: $sub";
}

Open in new window

Outputs:
INPUT NUMBER: 1, MOD: 1, SUBSTR: 1
INPUT NUMBER: 123, MOD: 3, SUBSTR: 3
INPUT NUMBER: 0, MOD: 0, SUBSTR: 0
INPUT NUMBER: 42, MOD: 2, SUBSTR: 2
INPUT NUMBER: 3.1415926, MOD: 3, SUBSTR: 6
INPUT NUMBER: 1000000009, MOD: 9, SUBSTR: 9
INPUT NUMBER: 1,007, MOD: 1, SUBSTR: 7

This is an artifact of PHP type juggling.

HTH, ~Ray
0
 
LVL 12

Expert Comment

by:North2Alaska
ID: 39751690
The question would be do you want the zero or a digit 1-9?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:St_Aug_Beach_Bum
ID: 39752069
I'd want 0 through 9 - looks like substr it is :)

Thank you both!

Chris
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 39752071
Thanks for running the test on these Ray -
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39752135
Thanks for the points and thanks for using EE.  I always find it's better to run a quick test than to depend on anything in the PHP funhouse :-)  Happy New Year, ~Ray
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question