Java/JSP 中字符串到整数的转换
我在 DB2 表中将变量 cost
定义为 String
。我将它放入一个值对象中,它也被定义为 String
。我需要检查来自 DB2 的值是否仅为空格。如果只是空格,我需要将0移进去。我还需要从中删除前导零。我可能会得到 cost
为 0000123
。但在 JSP 中我需要将其显示为 123
。你是怎么做到的?我将 vo 数据存储到会话变量中,并使用它在 JSP 中显示数据。
I have a variable cost
defined in a DB2 table as String
. I'm getting it into a value object where it is defined as a String
as well. I need to check if the value coming in from DB2 is only spaces. If it is only spaces, I need to move 0 into it. I also need to remove leading zeros from it. I may get cost
as 0000123
. But in JSP I need to display it as 123
. How do you do that? I'm storing the vo data into a session variable and using this, I'm displaying the data in JSP.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会考虑更改您的数据库架构以将值存储在数字字段中。如果您不能这样做,请考虑更改值对象以将其存储为数字字段并解析从数据库检索的字符串。
I would consider changing your database schema to store the value in a numeric field. If you can't do that, consider changing the value object to store it as a numeric field and parse the string you retrieve from the database.
了解您所描述的大部分内容听起来都是极其糟糕的设计,您可以继续该路径并使用 scriptlet。以下示例使用 Apache Commons Lang 来完成您的任务:
Understanding that most of what you have described sounds like extremely poor design, you can continue the path and use a scriptlet. The following example uses Apache Commons Lang to accomplish your task:
如果您无法更改数据库。使用
Integer.parseInt
(javadoc)。 (假设我们在这里处理整数,否则使用Double
中的等效项)。创建一个函数来处理字符串数字并在 JSP 中使用它。If you can't change your database. Use
Integer.parseInt
(javadoc). (This is assuming we are dealing with integers here, otherwise use the equivalent inDouble
). Create a function to process your String number and use it in your JSP.