com.github.TKnudsen.ComplexDataObject.model.tools.StringUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of complex-data-object Show documentation
Show all versions of complex-data-object Show documentation
A library that models real-world objects in Java, referred to as ComplexDataObjects. Other features: IO and preprocessing of ComplexDataObjects.
The newest version!
package com.github.TKnudsen.ComplexDataObject.model.tools;
/**
*
* Title: StringUtils
*
*
*
* Description: little helpers when working with String.
*
*
*
* Copyright: Copyright (c) 2017
*
*
* @author Juergen Bernard
* @version 1.02
*/
public class StringUtils {
public static void main(String[] args) {
double value = 66.3363456;
System.out.println(value);
String trunc = StringUtils.truncateDouble(value, 3);
System.out.println(trunc);
}
public static String truncateDouble(double value, int decimals) {
if (decimals < 0)
return null;
String result = (new Double(value)).toString();
int dot = result.lastIndexOf(".");
int length = result.length();
result = result.substring(0, (int) (Math.min(dot + 1 + decimals, length)));
return result;
}
}