JAVA字符串格式化-String.format()的使用

【Java】String Format 本文将介绍以下两个format 一个是String类中的format静态方法(重点介绍) 另一个是java.text.Format抽象基类 一、String类中的format方法 String.format() 是JDK1.5中,String类新增的一个静态方法,用来格式化的字符串。 静态方法: #使用指定的区域设置,格式字符串和参数返回格式化的字符串。 format(Loca... 阅读详情

JAVA字符串格式化-String.format()的使用

常规类型的格式化

String类的format()方法用于创建格式化的字符串以及连接多个字符串对象。熟悉C语言的同学应该记得C语言的sprintf()方法,两者有类似之处。format()方法有两种重载形式。

format(String format, Object... args) 新字符串使用本地语言环境,制定字符串格式和参数生成格式化的新字符串。

format(Locale locale, String format, Object... args) 使用指定的语言环境,制定字符串格式和参数生成格式化的字符串。

显示不同转换符实现不同数据类型到字符串的转换,如图所示

转  换  符

说    明 

示    例

%s

字符串类型

"mingrisoft"

%c

字符类型

'm'

%b

布尔类型

true

%d

整数类型(十进制)

99

%x

整数类型(十六进制)

FF

%o

整数类型(八进制)

77

%f

浮点类型

99.99

%a

十六进制浮点类型

FF.35AE

%e

指数类型

9.38e+5

%g

通用浮点类型(f和e类型中较短的)

 

%h

散列码

 

%%

百分比类型

%

%n

换行符

 

%tx

日期与时间类型(x代表不同的日期与时间转换符

 

 

复制代码

public static void main(String[] args) {  
    String str=null;  
    str=String.format("Hi,%s", "王力");  
    System.out.println(str);  
    str=String.format("Hi,%s:%s.%s", "王南","王力","王张");            
    System.out.println(str);                           
    System.out.printf("字母a的大写是:%c %n", 'A');  
    System.out.printf("3>7的结果是:%b %n", 3>7);  
    System.out.printf("100的一半是:%d %n", 100/2);  
    System.out.printf("100的16进制数是:%x %n", 100);  
    System.out.printf("100的8进制数是:%o %n", 100);  
    System.out.printf("50元的书打8.5折扣是:%f 元%n", 50*0.85);  
    System.out.printf("上面价格的16进制数是:%a %n", 50*0.85);  
    System.out.printf("上面价格的指数表示:%e %n", 50*0.85);  
    System.out.printf("上面价格的指数和浮点数结果的长度较短的是:%g %n", 50*0.85);  
    System.out.printf("上面的折扣是%d%% %n", 85);  
    System.out.printf("字母A的散列码是:%h %n", 'A');  
}  

复制代码

输出结果:

复制代码

Hi,王力  
Hi,王南:王力.王张  
字母a的大写是:A   
3>7的结果是:false   
100的一半是:50   
100的16进制数是:64   
100的8进制数是:144   
50元的书打8.5折扣是:42.500000 元  
上面价格的16进制数是:0x1.54p5   
上面价格的指数表示:4.250000e+01   
上面价格的指数和浮点数结果的长度较短的是:42.5000   
上面的折扣是85%   
字母A的散列码是:41

复制代码

 

 

 

搭配转换符的标志

如图所示:

标    志

说    明

示    例

结    果

+

为正数或者负数添加符号

("%+d",15)

+15

−

左对齐

("%-5d",15)

|15   |

0

数字前面补0

("%04d", 99)

0099

空格

在整数之前添加指定数量的空格

("% 4d", 99)

|  99|

,

以“,”对数字分组

("%,f", 9999.99)

9,999.990000

(

使用括号包含负数

("%(f", -99.99)

(99.990000)

#

如果是浮点数则包含小数点,如果是16进制或8进制则添加0x或0

("%#x", 99)

("%#o", 99)

0x63

0143

< 

格式化前一个转换符所描述的参数

("%f和%<3.2f", 99.45)

99.450000和99.45

$

被格式化的参数索引

("%1$d,%2$s", 99,"abc")

99,abc

 

复制代码

public static void main(String[] args) {  
    String str=null;  
    //$使用  
    str=String.format("格式参数$的使用:%1$d,%2$s", 99,"abc");             
    System.out.println(str);                       
    //+使用  
    System.out.printf("显示正负数的符号:%+d与%d%n", 99,-99);  
    //补O使用  
    System.out.printf("最牛的编号是:%03d%n", 7);  
    //空格使用  
    System.out.printf("Tab键的效果是:% 8d%n", 7);  
    //.使用  
    System.out.printf("整数分组的效果是:%,d%n", 9989997);  
    //空格和小数点后面个数  
    System.out.printf("一本书的价格是:% 50.5f元%n", 49.8);  
}  

复制代码

 

输出结果

复制代码

格式参数$的使用:99,abc  
显示正负数的符号:+99与-99  
最牛的编号是:007  
Tab键的效果是:       7  
整数分组的效果是:9,989,997  
一本书的价格是:                                          49.80000元

复制代码

 

 

 

日期和事件字符串格式化

在程序界面中经常需要显示时间和日期,但是其显示的 格式经常不尽人意,需要编写大量的代码经过各种算法才得到理想的日期与时间格式。字符串格式中还有%tx转换符没有详细介绍,它是专门用来格式化日期和时 间的。%tx转换符中的x代表另外的处理日期和时间格式的转换符,它们的组合能够将日期和时间格式化成多种格式。

常见日期和时间组合的格式,如图所示。

转  换  符

说    明

示    例

c

包括全部日期和时间信息

星期六 十月 27 14:21:20 CST 2007

F

“年-月-日”格式

2007-10-27

D

“月/日/年”格式

10/27/07

r

“HH:MM:SS PM”格式(12时制)

02:25:51 下午

T

“HH:MM:SS”格式(24时制)

14:28:16

R

“HH:MM”格式(24时制)

14:28

 

复制代码

public static void main(String[] args) {  
    Date date=new Date();                                  
    //c的使用  
    System.out.printf("全部日期和时间信息:%tc%n",date);          
    //f的使用  
    System.out.printf("年-月-日格式:%tF%n",date);  
    //d的使用  
    System.out.printf("月/日/年格式:%tD%n",date);  
    //r的使用  
    System.out.printf("HH:MM:SS PM格式(12时制):%tr%n",date);  
    //t的使用  
    System.out.printf("HH:MM:SS格式(24时制):%tT%n",date);  
    //R的使用  
    System.out.printf("HH:MM格式(24时制):%tR",date);  
}  

复制代码

输出结果:

复制代码

全部日期和时间信息:星期一 九月 10 10:43:36 CST 2012  
年-月-日格式:2012-09-10  
月/日/年格式:09/10/12  
HH:MM:SS PM格式(12时制):10:43:36 上午  
HH:MM:SS格式(24时制):10:43:36  
HH:MM格式(24时制):10:43  

复制代码

 

定义日期格式的转换符可以使日期通过指定的转换符生成新字符串。这些日期转换符如图所示。

复制代码

public static void main(String[] args) {  
    Date date=new Date();                                      
    //b的使用,月份简称  
    String str=String.format(Locale.US,"英文月份简称:%tb",date);       
    System.out.println(str);                                                                              
    System.out.printf("本地月份简称:%tb%n",date);  
    //B的使用,月份全称  
    str=String.format(Locale.US,"英文月份全称:%tB",date);  
    System.out.println(str);  
    System.out.printf("本地月份全称:%tB%n",date);  
    //a的使用,星期简称  
    str=String.format(Locale.US,"英文星期的简称:%ta",date);  
    System.out.println(str);  
    //A的使用,星期全称  
    System.out.printf("本地星期的简称:%tA%n",date);  
    //C的使用,年前两位  
    System.out.printf("年的前两位数字(不足两位前面补0):%tC%n",date);  
    //y的使用,年后两位  
    System.out.printf("年的后两位数字(不足两位前面补0):%ty%n",date);  
    //j的使用,一年的天数  
    System.out.printf("一年中的天数(即年的第几天):%tj%n",date);  
    //m的使用,月份  
    System.out.printf("两位数字的月份(不足两位前面补0):%tm%n",date);  
    //d的使用,日(二位,不够补零)  
    System.out.printf("两位数字的日(不足两位前面补0):%td%n",date);  
    //e的使用,日(一位不补零)  
    System.out.printf("月份的日(前面不补0):%te",date);  

复制代码

输出结果

 

复制代码

英文月份简称:Sep  
本地月份简称:九月  
英文月份全称:September  
本地月份全称:九月  
英文星期的简称:Mon  
本地星期的简称:星期一  
年的前两位数字(不足两位前面补0):20  
年的后两位数字(不足两位前面补0):12  
一年中的天数(即年的第几天):254  
两位数字的月份(不足两位前面补0):09  
两位数字的日(不足两位前面补0):10  
月份的日(前面不补0):10  

复制代码

和日期格式转换符相比,时间格式的转换符要更多、更精确。它可以将时间格式化成时、分、秒甚至时毫秒等单位。格式化时间字符串的转换符如图所示。

 

 

转  换  符

说    明

示    例

H

2位数字24时制的小时(不足2位前面补0)

15

I

2位数字12时制的小时(不足2位前面补0)

03

k

2位数字24时制的小时(前面不补0)

15

l

2位数字12时制的小时(前面不补0)

3

M

2位数字的分钟(不足2位前面补0)

03

S

2位数字的秒(不足2位前面补0)

09

L

3位数字的毫秒(不足3位前面补0)

015

N

9位数字的毫秒数(不足9位前面补0)

562000000

p

小写字母的上午或下午标记

中:下午

英:pm

z

相对于GMT的RFC822时区的偏移量

+0800

Z

时区缩写字符串

CST

s

1970-1-1 00:00:00 到现在所经过的秒数

1193468128

Q

1970-1-1 00:00:00 到现在所经过的毫秒数

1193468128984

 

 

复制代码

public static void main(String[] args) {  
    Date date = new Date();  
    //H的使用  
    System.out.printf("2位数字24时制的小时(不足2位前面补0):%tH%n", date);  
    //I的使用  
    System.out.printf("2位数字12时制的小时(不足2位前面补0):%tI%n", date);  
    //k的使用  
    System.out.printf("2位数字24时制的小时(前面不补0):%tk%n", date);  
    //l的使用  
    System.out.printf("2位数字12时制的小时(前面不补0):%tl%n", date);  
    //M的使用  
    System.out.printf("2位数字的分钟(不足2位前面补0):%tM%n", date);  
    //S的使用  
    System.out.printf("2位数字的秒(不足2位前面补0):%tS%n", date);  
    //L的使用  
    System.out.printf("3位数字的毫秒(不足3位前面补0):%tL%n", date);  
    //N的使用  
    System.out.printf("9位数字的毫秒数(不足9位前面补0):%tN%n", date);  
    //p的使用  
    String str = String.format(Locale.US, "小写字母的上午或下午标记(英):%tp", date);  
    System.out.println(str);   
    System.out.printf("小写字母的上午或下午标记(中):%tp%n", date);  
    //z的使用  
    System.out.printf("相对于GMT的RFC822时区的偏移量:%tz%n", date);  
    //Z的使用  
    System.out.printf("时区缩写字符串:%tZ%n", date);  
    //s的使用  
    System.out.printf("1970-1-1 00:00:00 到现在所经过的秒数:%ts%n", date);  
    //Q的使用  
    System.out.printf("1970-1-1 00:00:00 到现在所经过的毫秒数:%tQ%n", date);  
}  

复制代码

输出结果

复制代码

2位数字24时制的小时(不足2位前面补0):11  
2位数字12时制的小时(不足2位前面补0):11  
2位数字24时制的小时(前面不补0):11  
2位数字12时制的小时(前面不补0):11  
2位数字的分钟(不足2位前面补0):03  
2位数字的秒(不足2位前面补0):52  
3位数字的毫秒(不足3位前面补0):773  
9位数字的毫秒数(不足9位前面补0):773000000  
小写字母的上午或下午标记(英):am  
小写字母的上午或下午标记(中):上午  
相对于GMT的RFC822时区的偏移量:+0800  
时区缩写字符串:CST  
1970-1-1 00:00:00 到现在所经过的秒数:1347246232  
1970-1-1 00:00:00 到现在所经过的毫秒数:1347246232773  

复制代码

 

 

标签: JAVA

Java中 String.format()、MessageFormat.format()、StrUtil.format() 格式化拼接字符串使用介绍 2、MessageFormat.format() 也是格式化作用,但 MessageFormat.format() 使用的是标准占位符,且占位符必须指定参数索引,否则无法格式化。【1】String.format() 需要指定格式化数据类型,例如字符串(%s)、整数(%d)、小数(%f)、日期时间(%f)【1】日常简单字符串拼接,使用:StrUtil.format(),更简洁,支持指定索引占位符。(1)、这个例子中,使用两个占位符 %tF 和 %tT,分别代表要格式化的日期和时间。 阅读详情

相关推荐

NXP ChessDE的安装和激活

如果客户申请的是试用版,那么一份为期 30 天的 License 将会发送申请人的账号或邮箱中,客户使用该文件进行安装即可。② 将“VENDOR snpslmd /path/to/snpslmd”中的“/path/to/snpslmd”替换成 snpslmd.exe 实际存放路径(位于 SCL 安装目录下的 win32\bin 文件夹中,默认安装的情况下就是“VENDOR snpslmd C:\Synopsys\SCL\<version>\win32\bin\snpslmd.exe”)。

weixin_47351160的博客 2053

Java字符串格式化 String.format() 字符串格式化的详细用法

文章目录引言一、String.format()介绍常用的类型二、举例说明搭配转换符的标志日期和事件字符串格式化 引言 JAVA字符串格式化,String类的format()方法用于创建格式化的字符串以及连接多个字符串对象。 一、String.format()介绍 String.format()字符串常规类型格式化的两种重载方式: // 使用本地语言环境,制定字符串格式和参数生成格式化的新字符串 String str = String.format(String format, Object… arg

bingxuebojue的博客 4万+

EPLAN二次开发入门:Visual Studio 2019环境配置与插件调试实战

本文详细介绍了EPLAN二次开发入门的关键步骤,重点讲解如何在Visual Studio 2019中配置开发环境并进行插件调试。通过创建符合命名规范的项目、引用核心API库、实现插件接口以及使用附加进程调试等实战技巧,帮助电气工程师快速掌握EPLAN插件开发,提升自动化设计效率。

h6i7j8的博客 765

java中的String.format()方法详解

是 Java 中的一个字符串格式化方法,它用于生成指定格式的字符串。这个方法可以接受一个或多个参数,并将它们按照指定的格式插入到字符串中。它使用了类似于 C 语言中的printf函数的语法。其中,format是一个包含格式说明符的字符串,用于指定输出的格式,args是一个或多个要插入到格式字符串中的参数。// 输出:My name is John and I am 25 years old在这个示例中,%s是一个字符串格式说明符,表示要插入一个字符串;%d是一个整数格式说明符,表示要插入一个整数。

技术人集结地 2万+

图像去模糊(二)——Richardson–Lucy算法

Richardson Lucy Algorithm(Richardson-Lucy算法详解及效果实例)

xueruhongchen的博客 3万+

逆向拆解dy接口a_bogus 1.0.1.19版本核心算法(纯算版)第二部分

本文深入解析了dy接口a_bogus 1.0.1.19版本的核心算法,重点介绍了逆向分析的基础方法论、魔改Base64编码识别与还原、随机数生成与位运算解析等关键技术。通过详细的代码示例和实用技巧,帮助开发者掌握逆向工程中的关键步骤,特别是针对a_bogus参数的生成逻辑和混淆处理。

weixin_29288313的博客 425

Java 格式化之使用 %02X 格式化mac地址

记录下使用 %02X 格式化mac地址的方法。传入 %XYX 的意思,拆解如下,以 %X,%2X, %02X 为例,可以同时格式化多个,

weixin_44021334的博客 3526

SCRFD算法解读及环境安装实践

SCRFD检测算法及环境安装

lishanlu136的博客 1013

【Java】Java 中格式化字符串:String.format() 方法

虽然适合调试和显示简单消息,但它不太适合格式化字符串。格式化字符串不仅显示字符串内容,而且还按照指定的顺序显示内容。例如,当显示像100000000这样的大整数时,您可能需要包含逗号,以便它显示为。与小数类似,您可能希望显示特定的小数位数,例如199.53以及四舍五入。程序员会很高兴知道 Java 提供了一些格式化方法,并为各种数据类型(例如DoubleInteger和Date)提供了充足的支持。在 Java 中格式化字符串有三种主要方法。您可以使用**String.format()**方法、

Etui۹(・༥・´)و的博客 1万+

io_uring vs epoll ,谁在网络编程领域更胜一筹?

简介:从定量分析的角度,通过量化 io_uring 和 epoll 两种编程框架下的相关操作的耗时,来分析二者的性能差异。 本文作者:王小光,「高性能存储技术SIG」核心成员。 背景 io_uring 在传统存储 io 场景已经证明其价值,但 io_uring 不仅支持传统存储 io,也支持网络 io。io_uring 社区有众多的开发者尝试将 io_uring 用于网络应用。我们之前也在《你认为 io_uring 只适用于存储 IO?大错特错!》中也探索过 io_uring 在网络场景的应用及其

阿里云云栖号 3041

Python爬取拼多多商品数据保姆级教程(附源码及效果图)

本文介绍了使用Python爬取拼多多商品数据的完整教程。通过DrissionPage库模拟浏览器操作,利用正则表达式提取嵌入在HTML中的商品数据(关键步骤是从window.rawData变量获取),解析后转换为结构化数据并导出Excel。教程包含八大步骤:环境配置、浏览器初始化、网络监听、搜索模拟、数据提取(核心是正则匹配JSON)、数据验证、字段解析和Excel导出,最后提供了封装好的函数可直接调用。该方法特别适合课程作业或毕业设计获取电商数据,能爬取商品名称、价格、销量等关键信息,处理了价格单位转换等

weixin_46449236的博客 2098

JAVA字符串格式化——String.format()的使用

String类的format()方法用于创建格式化的字符串以及连接多个字符串对象。熟悉C语言应该记得C语言的sprintf()方法,两者有类似之处。format()方法有两种重载形式。

qq_43842093的博客 2375

让 ABAP 开发者也能做出高质量 SAP Fiori:到底需要掌握多少前端知识?

文章摘要: 随着SAP技术演进,ABAP开发者的角色边界正在重构。传统SAP GUI时代的全栈开发模式逐渐转向RAP+Fiori Elements的新范式,其中关键技术变化包括: 开发模式转变:从前后端耦合转向RAP模型驱动开发,通过CDS注解实现UI意图表达 分工优化:Fiori Elements模板处理80%标准UI需求,Flexible Programming Model解决20%差异化需求 效率提升:注解驱动开发可在RAP建模阶段同步定义UI结构,大幅缩短交付周期 工具整合:SAP提供从建模到部署的

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 357

JAVA字符串格式化-String.format()的使用详解

在的 亲 您好 有什么可以帮助您的呢?

黄文孝同学的博客 4903

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

java url 格式化_String.format()的使用:Java字符串格式化

今天技术之家陪你一起了解Java开发:String.format()的使用:Java字符串格式化1 问题背景当我们在开发中需要格式化时或者当一字符串中有某一部分是需要变化时,我们应该如何快速的进行处理呢?比如处理批量的url中www.xxx.com/s?index=后面需要带参数时。JDK1.5开始String类中提供了一个非常有用的方法String.format(String format, O...

weixin_42123237的博客 1711

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

java的字符串格式化——String.format()和System.out.printf()的使用

引言 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象。熟悉C语言应该记得C语言的sprintf()方法,两者有类似之处。format()方法有两种重载形式。 重载 // 使用当前本地区域对象(Locale.getDefault()),制定字符串格式和参数生成格式化的字符串 String String.format(String fmt, Objec...

小宇飞刀的BLOG 4264

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

java 格式化字符串 三位_JAVA字符串格式化-String.format()的使用

本篇介绍JAVA字符串格式化-String.format()的使用,具体如下:常规类型的格式化String类的format()方法用于创建格式化的字符串以及连接多个字符串对象。熟悉C语言的同学应该记得C语言的sprintf()方法,两者有类似之处。format()方法有两种重载形式。format(String format, Object... args) 新字符串使用本地语言环境,制定字符串格式...

weixin_39894473的博客 998

java字符串格式化:String.format()方法的使用

常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象。熟悉C语言的读者应该记得C语言的sprintf()方法,两者有类似之处。format()方法有两种重载形式。 l format(String format, Object... args) 该方法使用指定的字符串格式和参数生成格式化的新字符串。 新字符串始终使用本地语言环境。例如当前日期...

沐 白 755
上一篇: 有关Hikaricp连接池配置的解读
下一篇: 删除文件后,磁盘可用空间并没有释放怎么办?
王树民
博客等级 码龄10年 912粉丝 560原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址