How do I parse a number from a String that may have a leading zero?
I'm not familiar with regexes, so forgive me if this answer's off-base. I've been assuming that $3, $2, and $1 are strings. Here's what I did in IRB to replicate the problem:
irb(main):003:0> Integer("04")
=> 4
irb(main):004:0> Integer("09")
ArgumentError: invalid value for Integer: "09"
from (irb):4:in `Integer'
from (irb):4
from :0
But it looks like .to_i doesn't have the same issues:
irb(main):005:0> "04".to_i
=> 4
irb(main):006:0> "09".to_i
=> 9
Java ParseInt() - Catching Strings with a leading zero
Check if the first character is 0 :
if (original.charAt(0)=='0')
or
if (original.startsWith("0"))
If the String starts with a 0, you don't want to call parseInt
at all.
I think that comparing a single character is more efficient than using regular expressions.
Parse String to long with leading zero
You cannot because a long does not have a leading zero. A long is supposed to store integers (the mathematical concept, not int
), i.e.
A string of characters like 05
is not an integer, 5
is. What you can do is format a long that holds 5
with a leading zero when you print it, see e.g. java.util.Formatter.
Are you sure you even want to have a long/an integer? What do you want to do with it?
How to extract numbers with leading zeros from mixed Excel cells?
Assuming your text is ####[space]xxxx
, you can pull the numbers with LEFT()
and SEARCH()
:
=TRIM(LEFT(A1;SEARCH(" ";A1)-1))
How to convert String to Long without losing leading zero
You don't, a Long
/ long
/ Integer
/ int
is a number, a number does not having leading zeros. Only a string representation of a number may have leading zeros.
If you absolutely need the leading zeros and the number 0001 is different from 001 then you are not dealing with numbers but just with strings that look like numbers and you should not convert it to a long in the first place.
How do I retain leading zeroes in an Integer/Number in JavaScript?
You can't have a number with leading zeroes in Javascript, because, as Randy Casburn said, they don't have any value. You have to convert it to a string and use String.padStart()
to pad the string with zeroes. parseInt
will work with leading zeroes. For example:
(294).toString().padStart(6, "0") --> "000294"
parseInt("000294") --> 294
covert number strings with leading zeros to numeric value with leading zero in postgres 12
Numbers don't have leading zeros. 0480
is exactly the same number as 480
- just like 00000001
is exactly the same number as 1
The only way to get leading zeros, is to convert the number to a string and add the desired number of leading zeros.
some outputs of to_char function, little bit confusing?
Not, not at all.
to_char(0480,'9999')
converts the number 480
to a 4 character string. The 9
indicates that if there is no value for that digit, it can be "dropped".
Quote from the manual
9 - digit position (can be dropped if insignificant)
0 - digit position (will not be dropped, even if insignificant)
Leading zeros are always insignificant (from a mathematical perspective).
to_char(480,'0000')
will do you what you expect: a four digit string with a leading zero.
Similarly to_char(1,'0000')
will return 0001
.
Note that by default, all output of to_char()
is right-aligned with leading spaces. If you don't want that, you should use the FM
modifier: e.g. to_char(1,'FM0000')
How to convert a string (with leading zero or not) to an integer?
There are several ways to convert a string to a number, I prefer to use the unary +
operator:
var number = +"08"; // 8
This is the equivalent of writing:
var number = Number("08"); // 8
Unlike parseInt()
, when using +
or Number()
no radix is necessary because the internal number conversion will not parse octal numbers. If you want the parseInt()
or parseFloat()
methods, it's also pretty simple:
var number = parseInt("08", 10); // 8
parseInt
and parseFloat
are less reliable for user input because an invalid numeric literal might be considered salvageable by these functions and return an unexpected result. Consider the following:
parseInt("1,000"); // -> 1, not 1000
+"1,000"; // -> NaN, easier to detect when there's a problem
Extra Reading
- Number() - MDC
- Converting to number (JavaScript Type-Conversion) - jibbering.com
Related Topics
Jekyll Templates Using Django-Like Liquid Blocks/Inheritance
How to Build Task 'Gems:Install'
Ruby: Multiply All Elements of an Array
User Theme Switching with SASS - Ruby on Rails
How to Find Out Why a Gem Bundle Has Locked a Gem at a Specific Version
Ruby String#Scan Equivalent to Return Matchdata
In Ruby, How to Remove Only 1 Match in an Array Easily
When Trying to Generate a Model with Rails and Postgresql, the Command Hangs Without Error
Sass: Dealing with The Ie 4095 Selectors Per Stylesheet Restriction
Installing Rails with Gem, Error Loading Command: Install Undefined Method 'Invoke_With_Build_Args'
How Write into CSV File Properly
Setting the Environment in Gemfile for Bundling Install/Update Based on a Customize File
Preventing Delayed_Job Background Jobs from Consuming Too Much CPU on a Single Server
Rbenv and Bundler: "Bad Interpreter: No Such File or Directory"
Rails Console Fails with 'Switch to Inspect Mode' in Windows
Run Selenium with Chrome Driver on Heroku: 'Cannot Find Chrome Binary'