目录
Java
java保留两位小数
1、使用String.format()方法:
public static void stringFormatdecimalFormatKeepTwoDecimalPlaces(){
double number = 3.1415926;
String result = String.format("%.2f", number);
System.out.println(result);
}
输出:3.14
2、BigDecimal保留两位小数
import java.math.BigDecimal;
public static void bigdecimalKeepTwoDecimalPlaces(){
double number = 3.1415926;
BigDecimal decimal = new BigDecimal(number);
BigDecimal rounded = decimal.setScale(2, BigDecimal.ROUND_HALF_UP);
System.out.println(rounded);
}
输出:3.14
3、使用DecimalFormat类:
public static void decimalFormatKeepTwoDecimalPlaces(){
double number = 3.1415926;
DecimalFormat decimalFormat = new DecimalFormat("#.00");
String result = decimalFormat.format(number);
System.out.println(result);
}
输出:3.14
基本数据类型
/**
* 基本数据类型,在定义的时候根据不同的类型在内存中分配不同的大小进行存储。
*
*
* 整数:
* byte(1字节byte=8位)
* 1000 0000 ~ 01111111 最高位是符号位0表示正 1表示负
* byte值的范围:-128 ~ 127
*
*
* short(2字节) -32768~32767
*
* \ int(4字节) -2,147,483,648 ~ 21 4748 3647(21亿多)
*
* \ long(8字节)
*
* 浮点型:
* float(4字节) 定义float类型变量时,变量要以"f"或"F"结尾
* 单精度浮点数 提供大约6-7位的有效数字。
*
* \ double(8字节)
* 双精度浮点数 提供大约15-16位的有效数字。
* 推荐使用double,因为现代计算机硬件通常更擅长处理双精度浮点数。
*
* 简单来说double精度更高,可以表示更细的浮点数,通俗的讲就是小数点后面可以表示更多的数字。
*
* 布尔型:
* boolean(1字节)
*
* 字符型:
* char(2字节)
*
*
*/
/**
* 运算
*
* 整数除法运算,结果是取整
* 浮点数除整数 结果为 浮点数
* 整数 除 浮点数 结果为 浮点数
*
* 文件大小 23435.12 MB
*
* 速率 = 文件大小 / 用时
* 用时
*/
@Test
public void test3(){
int fileSizeB = 46960880; // 44.7 MB
int fileSizeMB = fileSizeB / 1024 / 1024;
System.out.println(fileSizeMB);// 44 整数除整数结果也是取整
double fileSizeMB1 = fileSizeB / 1024 / 1024;
System.out.println(fileSizeMB1); // 44.0 只是将整数赋值给了一个浮点数double
double fileSizeB1 = 46960880;
double fileSizeMB2 = fileSizeB1 / 1024 / 1024;
System.out.println(fileSizeMB2);// 44.78538513183594 浮点数除整数 结果为 浮点数
// 结果保留一位小数
System.out.println(String.format("%.1f", fileSizeMB2));// 44.8 会自动四舍五入
DecimalFormat decimalFormat = new DecimalFormat("#.0");
System.out.println(decimalFormat.format(fileSizeMB2));// 44.8 会自动四舍五入
// 向下截断小数部分
System.out.println(Math.floor(fileSizeMB2));// 44.0
// 向上截断小数部分
System.out.println(Math.ceil(fileSizeMB2));// 45.0
// 保留一位小数,不进行四舍五入
System.out.println((int) (fileSizeMB2 * 10));
double truncatedNumber = (int) (fileSizeMB2 * 10) / 10.0; // 整数 除 浮点数 结果为 浮点数
System.out.println(truncatedNumber);// 44.7
int mil = 23435; // 毫秒
double second = mil / 1000.0;
System.out.println(second);
double speed = (int)((fileSizeMB2 / second) * 10) / 10.0;
System.out.println(speed + " MB/S");// 1.9 MB/S
}
分页查询
https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>
yml配置
pagehelper:
helperDialect: mysql或者oracle
supportMethodsArguments: true
params: count=countSql
helperDialect: mysql
:指定了数据库方言为 MySQL。supportMethodsArguments: true
:开启了支持方法参数,允许在方法中传递参数进行分页。params: count=countSql
:通过传递count
参数来执行 countSql,这通常是用于执行查询总记录数的 SQL。
// 分页
if(highSearchVO.getPageNo() != null) PageHelper.startPage(highSearchVO.getPageNo(),highSearchVO.getPageSize());
List<Map<String, Object>> maps = dynamicDataRetrievalMapper.highSearch(highSearchVO);
在需要分页查询之前调用PageHelper.startPage设置,会自动limit
websocket实时通信
业务中,客户端和服务端经常需要实时返回一些状态等消息,现总结一下websocket实时通信用法。
服务端
服务端一般用来接收客户端的ws连接,然后给客户端发送消息,不能主动发送连接。
<!--ws-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
* 开启WebSocket
*/
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
package com.lin.ws;
/**
*
*/
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lin.entity.vo.UploadMsgVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;
import javax.swing.Timer;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
@Component
@ServerEndpoint("/webSocket/{topic}")
@Slf4j
public class WebSocketServer {
private Session session;
private String topic;
/**静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。*/
private static int onlineCount = 0;
private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<>();
private static List<UploadMsgVO> uploadMsgList = new ArrayList<>(3);
private javax.swing.Timer timer = new Timer(3000, e -> {
sendMessage(JSONObject.toJSONString(uploadMsgList),"upload");
uploadMsgList.clear();
});
/**
* concurrent包的线程安全set,用来存放每个客户端对应的MyWebSocket对象
*/
private static ConcurrentHashMap<String,WebSocketServer> webSocketMap = new ConcurrentHashMap();
/**
* 保存ws会话
* 为了保存在线用户信息,在方法中新建一个list存储一下【实际项目依据复杂度,可以存储到数据库或者缓存】
*/
private final static List<Session> SESSIONS = Collections.synchronizedList(new ArrayList<>());
/**
* 建立连接
* ws://192.168.31.47:9988/webSocket/{topic}
* 当客户端发送: ws://192.168.31.47:9988/webSocket/upload ws请求,就可以和这个ws服务端建立ws连接了。
*
* @param session ws连接会话
* @param topic ws连接主题
*/
@OnOpen
publ