表示电话号码的正确方法是什么?
我在我的一个应用程序中表示手机号码时遇到问题。
我想知道是否有一个 Integer 类允许您存储以 0417254482 开头的数字。也许使用字符串更合适? 目前,当我尝试使用整数、双长整数表示电话号码时,我似乎存储了随机数,而不是我想要存储的数字。
I'm having trouble representing a mobile number in one of my applications.
I was wondering if there is an Integer class that will allow you to store such a number starting with 0417254482. Perhaps using a string be a more appropriate?
At present when I'm trying to use represent a phone number with ints, doubles longs I seem to store random numbers and not the numbers I meant to store.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
使用
字符串
。除此之外,如果使用整数,您将无法存储前导零。您绝对不应该使用int
(太小)float
或double
(数据丢失的风险太大 -见下文);long
或BigInteger
可能是合适的(除了前导零问题),但坦率地说,我会选择String
。这样,如果您愿意,您还可以存储用户输入的任何破折号或空格,以便更容易记住该数字。就上面提到的
float
和double
的“数据丢失”而言 -float
肯定没有足够的精度;double
可以工作,如果你很高兴你永远不需要超过 16 位数字(比使用long
得到的数字少几个),但是您需要非常非常小心,无论在哪里将值从double
转换回string
,您都会获得准确的值。许多格式转换将为您提供一个近似值,该近似值可能精确到 10 位有效数字 - 但您需要一个精确的整数。基本上,对电话号码使用浮点是一个根本上的坏主意。如果您必须使用固定宽度的数字类型,请使用long
,但理想情况下,完全避免使用它。Use
String
. Aside from anything else, you won't be able to store leading zeroes if you use integers. You definitely shouldn't useint
(too small)float
ordouble
(too much risk of data loss - see below);long
orBigInteger
could be appropriate (aside from the leading zeroes problem), but frankly I'd go withString
. That way you can also store whatever dashes or spaces the user has entered to make it easier to remember the number, if you want to.In terms of the "data loss" mentioned above for
float
anddouble
-float
definitely doesn't have enough precision;double
could work if you're happy that you'll never need more than 16 digits (a couple fewer than you get withlong
) but you would need to be very, very careful that anywhere you convert the value back fromdouble
tostring
, you got the exact value. Many formatting conversions will give you an approximation which may be accurate to, say, 10 significant digits - but you'd want an exact integer. Basically, using floating point for phone numbers is a fundamentally bad idea. If you have to use a fixed-width numeric type, use along
, but ideally, avoid it entirely.想一想:电话号码真的是一个数字吗?与电话号码相加(或进行其他算术运算)是否有意义?电话号码是代码,它们通常用数字表示,但这只是一种约定,也许在另一个国家也使用字母(我刚刚意识到,国际电话号码怎么样?它们有一个
+< /code> 开头。
你必须思考你想要表现的事物的本质,然后找到最合适的表现形式。
Think about this: Is a phone number really a number? Does it make sense adding (or make another arithmetic operation) with phone numbers? Phone numbers are codes, they're usually represented with numbers, but that's just a convention and, maybe, in another country the use letters too (I've just realized, what about international phone numbers? they have a
+
at the beginning.You have to think about the nature of the things you want to represent, and then, find the most suitable representation.
如果您想进行验证和规范化,您可能需要依赖一个可以正确完成此操作的库。 https://github.com/googlei18n/libphonenumber 是最常见的选项之一。
If you want to do validation and normalization you probably want to rely on a library that does it properly for you. https://github.com/googlei18n/libphonenumber is one of the most common options.
尽管电话号码是命名号码,但它们通常不是数字(例如前导零、国家/地区前缀+XX,...)。
因此,在程序中正确表示电话号码有两种可能性:
String
来保留输入的整个号码。使用自定义数据类型为电话号码功能提供额外支持
查看所有不同的国际通用的惯例
Although phone numbers are named numbers, they are normally not numbers (e.g. leading zeros, country prefix +XX, ...).
So there are two possibilities to represent a phone number correctly inside a program:
String
to keep the whole number like entered.Using a custom data type that offers additional support for phone number features
It's really interesting to look at all the different conventions that are used internationally
创建您自己的 PhoneNumber 类,并使用 String 类型的私有字段来表示它。
如果所有电话号码的长度相同,您也可以用 long 或 BigInteger 表示号码,但要小心前导零。
电话号码实际上并不是整数(或字符串)。它是别的东西,应该有自己的一类。
编辑:
还有一件事:我不会为此类实现 setter,因为电话号码对象最好是不可变的
Create your own PhoneNumber class with a private field of type String to represent it.
You could also represnt the number with long or BigInteger if all phone numbers have the same length, but be careful with leading zeros.
A phone number is not really an integer (or a string). It is something else which shuld have a class of its own.
EDIT:
one more thing: I wouldn't implement a setter for this class because a phone number object would better be immutable
您应该使用字符串或更专业的数据结构。
主要原因是您可以对电话号码执行的操作是字典顺序的,而不是算术运算。
例如,您可以说法国的电话号码以
+33
开头,但您不能假设它们在数字范围内。我认为这些其他参数无效
*
或#
。这些符号可以在电话线上传输,但它们不是电话号码本身的一部分,我认为它超出了范围。+
开头只是E164号码的表示,以便与本地号码区分开来。如果您只操作 E164 数字,那么他们实际上不必这样做。.
、-
、等)。
You should use a string or a more specialized data structure.
The main reason is that the operations that you can do on phone numbers are lexicographic, and not arithmetic.
e.g. You can say that phone numbers for France start with
+33
, but you cannot assume they are in a numerical range.These other arguments are not valid in my opinion
*
or#
. This symbols can be transported on phone lines, but they are not part of the phone number itself, and I consider it's out of scope.+
is just a representation of E164 numbers, so that they can be distinguished from local numbers. They really don't have to, if you only manipulate E164 numbers..
,-
,, etc.).
您应该使用字符串来支持带有前导零的数字。您提供的代码是:
在构造函数中,数字是字符串,但是当您调用构造函数时,您使用了 int 来表示电话号码。我认为java中一定有编译错误。这和你编译的代码是一样的吗?
You should use string to support numbers with leading zeros. The code you provided was:
In the constructor, the number is string but when you call the constructor you used an int for phone number. There must have been a compile error here in java I think. Is this the same code you compiled?
每个数字的左侧和右侧都有无穷多个零,
要表示它,您应该使用字符串格式
使用 %010d 表示
%[argument_index$][flags][width][. precision]转换
标志 0 - 填充零
10 - 填充零的数量
d - 十进制整数
Comparable 接口的实现使您可以对 List 进行排序。
输出是
可比较
字符串格式化程序
Every number have infinity amount of zeros on the left and right side,
To represent it you should use a string formating
Used %010d mean
%[argument_index$][flags][width][.precision]conversion
flag 0 - padding zeros
10 - amount of padding zeros
d - decimal integer
The implementation of interface Comparable give you the posibility to sort List.
The output is
Comparable
String Formatter
我建议不要使用原始数据类型。
原因:基元不能接受特殊字符,例如+、-、(、、)。如果您接受此格式的电话号码+1(xxx)-xxx-xxxx。
I would suggest not use primitive data type.
Reason: Primitives cannot accept special characters such as +,-,(, and, ). If you accept phone numbers in this format +1(xxx)-xxx-xxxx.