博客
关于我
Java数据类型转换
阅读量:801 次
发布时间:2019-03-25

本文共 721 字,大约阅读时间需要 2 分钟。

<string转换与类型转换指南>

  • 将字符串转为整型:
    • <strong>一种常用的方法是使用Integer.parseInt方法</strong>int i = Integer.parseInt(String str);
    • another way 是通过Integer.valueOf获取对象然后调用intValue方法:int i = Integer.valueOf(String str).intValue();请注意,两个方法的实现方式不同。前者直接返回整型数值,后者需要通过对象调用方法获取结果。
    1. 整型转换为字符串:
      • 推荐使用String.valueOf方法:String str = String.valueOf(int i);
      • 也可以使用Integer.toString方法:String str = Integer.toString(int i);
      • 或者直接使用字符串拼接的方式:String str = “” + i ;
      1. float型转换为double型:
        • 创建Float对象并调用其doubleValue方法:float f1=100.00f; Float F1=new Float(f1); double d1=F1.doubleValue();
        1. double型转换为int型:
          • 通过创建Double对象并调用intValue方法:double d1=100.00; Double D1=new Double(d1); int i1=D1.intValue();
          1. int型转换为double型:
            • 直接赋值即可:double d1=i1; 这种方式简洁直观。

            以上转换方法可以灵活运用于实际开发中,确保数据类型转换正确无误。

    转载地址:http://mbjyk.baihongyu.com/

    你可能感兴趣的文章
    npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
    查看>>
    npm安装教程
    查看>>
    npm报错Cannot find module ‘webpack‘ Require stack
    查看>>
    npm报错Failed at the node-sass@4.14.1 postinstall script
    查看>>
    npm报错fatal: Could not read from remote repository
    查看>>
    npm报错File to import not found or unreadable: @/assets/styles/global.scss.
    查看>>
    npm报错TypeError: this.getOptions is not a function
    查看>>
    npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
    查看>>
    npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
    查看>>
    npm版本过高问题
    查看>>
    npm的“--force“和“--legacy-peer-deps“参数
    查看>>
    npm的安装和更新---npm工作笔记002
    查看>>
    npm的常用操作---npm工作笔记003
    查看>>
    npm的常用配置项---npm工作笔记004
    查看>>
    npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
    查看>>
    npm编译报错You may need an additional loader to handle the result of these loaders
    查看>>
    npm设置淘宝镜像、升级等
    查看>>
    npm设置源地址,npm官方地址
    查看>>
    npm设置镜像如淘宝:http://npm.taobao.org/
    查看>>
    npm配置安装最新淘宝镜像,旧镜像会errror
    查看>>