Java/JSP 中字符串到整数的转换

发布于 2024-10-14 13:56:49 字数 274 浏览 0 评论 0原文

我在 DB2 表中将变量 cost 定义为 String。我将它放入一个值对象中,它也被定义为 String 。我需要检查来自 DB2 的值是否仅为空格。如果只是空格,我需要将0移进去。我还需要从中删除前导零。我可能会得到 cost0000123。但在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彻夜缠绵 2024-10-21 13:56:49

我会考虑更改您的数据库架构以将值存储在数字字段中。如果您不能这样做,请考虑更改值对象以将其存储为数字字段并解析从数据库检索的字符串。

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.

烦人精 2024-10-21 13:56:49

了解您所描述的大部分内容听起来都是极其糟糕的设计,您可以继续该路径并使用 scriptlet。以下示例使用 Apache Commons Lang 来完成您的任务:

<%= org.apache.commons.lang.math.NumberUtils.toInt(org.apache.commons.lang.StringUtils.trimToNull(cost),0) %>

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:

<%= org.apache.commons.lang.math.NumberUtils.toInt(org.apache.commons.lang.StringUtils.trimToNull(cost),0) %>
夜深人未静 2024-10-21 13:56:49

如果您无法更改数据库。使用Integer.parseInt (javadoc)。 (假设我们在这里处理整数,否则使用 Double 中的等效项)。创建一个函数来处理字符串数字并在 JSP 中使用它。

<%
function String processNumber(String value){
  if(value == null || value.trim().length() == 0){
     value = "0"//use a zero if all we have is whitespace or if the value is null
  }

  int intValue = 0;//store the integer version (which won't have leading zeros)
  try{
    int intValue = Integer.parseInt(value);//process the number
  }
  catch(Exception e){
    intValue = 0;//use 0 if there's a problem
  }
  return "" + intValue;//return the String version free of leading zeros
}
%>
<p>Number: <%= processNumber(getValue()) //replace getValue with however you get your value %></p>

If you can't change your database. Use Integer.parseInt (javadoc). (This is assuming we are dealing with integers here, otherwise use the equivalent in Double). Create a function to process your String number and use it in your JSP.

<%
function String processNumber(String value){
  if(value == null || value.trim().length() == 0){
     value = "0"//use a zero if all we have is whitespace or if the value is null
  }

  int intValue = 0;//store the integer version (which won't have leading zeros)
  try{
    int intValue = Integer.parseInt(value);//process the number
  }
  catch(Exception e){
    intValue = 0;//use 0 if there's a problem
  }
  return "" + intValue;//return the String version free of leading zeros
}
%>
<p>Number: <%= processNumber(getValue()) //replace getValue with however you get your value %></p>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文