473,549 Members | 2,616 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how split number 3 parts

59 New Member
hi,
i have a number with 9 digits lenght
and i want split this number: 922888222
into this: 922 888 222
how can i do that?
thanks a lot for your help :)
Sep 30 '09 #1
9 12028
Dormilich
8,658 Recognized Expert Moderator Expert
you can use a RegEx (preg_replace()).
Sep 30 '09 #2
Markus
6,050 Recognized Expert Expert
@Dormilich
preg_split() would be better ;)

And str_split() better still - pay attention to the optional 'length' parameter.
Sep 30 '09 #3
Dormilich
8,658 Recognized Expert Moderator Expert
@Markus
that depends on the desired output.
Sep 30 '09 #4
Markus
6,050 Recognized Expert Expert
@Dormilich
I don't think the output is in question - the OP asked for his string to be split.
Sep 30 '09 #5
Dormilich
8,658 Recognized Expert Moderator Expert
the OP didn’t mention to split it into an array. you know how loose the meaning of a word can be in this forum.
Sep 30 '09 #6
TheServant
1,168 Recognized Expert Top Contributor
I am presuming that it is a phone number or the like that the OP wants formatted. How I would do it, not saying Dorms way won't work or be better, but str_split() and then a simple echo with spaces. It can be done in a loop if the length of the original number is variable, but I think this will do:
Expand|Select|Wrap|Line Numbers
  1. $number = "922888222";
  2. $array = str_split($number,3); // Splits the number into a new element every 3 characters
  3. echo( $array[0],' ', $array[1],' ', $array[2] ); // Ouputs each element with a space in between
Sep 30 '09 #7
zorgi
431 Recognized Expert Contributor
I would probably do it like this


Expand|Select|Wrap|Line Numbers
  1. number_format(922888222, 0, " ", " ");
  2.  
http://hr.php.net/number_format
Oct 1 '09 #8
TheServant
1,168 Recognized Expert Top Contributor
@zorgi
Great idea! Looks like it would work.
Oct 1 '09 #9
eladkarako
2 New Member
Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * @param float $size                - the memory size to format
  3.  * @param bool  $is_add_commas       (optional) - saperete every 3 digits from the end so 56789 will be "56,789".
  4.  * @param bool  $is_full_description (optional) - use *Bytes instead of *b (GigaBytes instead of gb, etc...).
  5.  * @param int   $digits              (optional) - number of digits decimal point, to limit.
  6.  * @return string
  7.  * @author http://icompile.eladkarako.com
  8.  */
  9. function human_readable_memory_sizes($size, $is_add_commas = false, $is_full_description = false, $digits = 20) {
  10.   $unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb');
  11.   $unit_full = array('Bytes', 'KiloByte', 'MegaBytes', 'GigaBytes', 'TeraBytes', 'PetaBytes');
  12.  
  13.   $out = $size / pow(1024, ($i = floor(log($size, 1024))));
  14.  
  15.   $out = sprintf("%." . $digits . "f", $out);
  16.  
  17.   $out = !$is_add_commas ? $out : preg_replace_callback('/(\d)(?=(\d{3})+$)/', function ($arr) {
  18.     return isset($arr[0]) ? "$arr[0]," : "";
  19.   }, $out);
  20.  
  21.   $out .= ' ' . (!$is_full_description ? $unit[ $i ] : $unit_full[ $i ]);
  22.  
  23.   return $out;
  24. }
  25.  
the preg_replace_ca llback is what you want (in PHP),
for JavaScript your can use:
Expand|Select|Wrap|Line Numbers
  1. function addCommas(t) {
  2.   return String(t).replace(/(\d)(?=(\d{3})+$)/g, "$1,")
  3. }

taken from:
first one:
http://icompile.eladkarako.com/php-s...ar-expression/

second one:
http://icompile.eladkarako.com/javas...ar-expression/

Elad.
Aug 1 '14 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2416
by: kinne | last post by:
The following code is supposed to reverse the date in "yyyy-mm-dd" format, but it produces different results in Firefox 1.0 and in Internet Explorer 6SP1. In Firefox, the result is correct ("2004-11-29") but it's wrong in Internet Explorer 6SP1 ("00:20:15-11-29"). If I change "dateParts" to "dateParts", it's exactly the opposite that occures:...
16
3375
by: nicolas | last post by:
Aim: scanf a number what random digit,split this number; for example: input "5680" output "5,6,8,0"; input "45" output "4,5";
11
2363
by: Jay | last post by:
Hey Guys, I need an algorithm/formula to do the following: I have two 32-bit timers cascaded to form a 64-bit timer, max value per timer(50sec). This is the way they work: value | timer1 | timer2 5 50 3 when timer1 counts down to zero, timer2 would count down to two.
19
4422
by: gk245 | last post by:
Trying to write a program that will figure out if a number is perfect or not. Here is my logic: 1) Read in the number 2) Split it up (number - 1) 3) Put all the split up numbers into an array 4) Figure out if the original number is evenly divisible by any of the numbers in the array.
2
1625
by: nani | last post by:
Hi friends I am creating one web application in that i take one hyperlink control.when i click hyperlink control.how to split the page in two parts and display the result in that secound part If the result is not there again in orginal format How to create it plz tell
13
27942
by: Chaim Krause | last post by:
I am unable to figure out why the first two statements work as I expect them to and the next two do not. Namely, the first two spit the sentence into its component words, while the latter two return the whole sentence entact. import string from string import whitespace mytext = "The quick brown fox jumped over the lazy dog.\n" print...
5
3010
by: Alex Hoilo | last post by:
Hello, I'm a novice to VBA and need to import raw data into an access database. I created the table, it contains 87 fields, all numeric. The data I have looks like this: 01390681068168120110301002936140400000000000000000 3000000100000000000000000000 0239068106816812011030100293603600541022867+000100...
0
7521
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7451
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7473
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6044
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5369
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1944
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
764
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.