number-formatting

Is there a Java number formatting library that handles significant digits?

心已入冬 提交于 2019-12-03 19:54:28
问题 The built in DecimalFormat only allows you to specify number of digits to the right of the decimal place. Because of the limitations of representing numbers as double, number formatting needs to include some level of rounding inside of it. In the general case, that rounding has to be to a number of significant digits (default case is the precision of a double) or else your formatted double will end up showing stuff like 3.5999999 instead of 3.6. The closest solution I could find is using new

How do I align the decimal point when displaying doubles and floats

孤街醉人 提交于 2019-12-03 12:09:29
If I have the following decimal point numbers: Double[] numbers = new Double[] {1.1233, 12.4231414, 0.123, 123.3444, 1.1}; for (Double number : numbers) { System.out.println(String.format("%4.3f", number)); } Then I get the following output: 1.123 12.423 0.123 123.344 1.100 What I want is: 1.123 12.423 0.123 123.344 1.100 The part that can be a bit confusing is that String.format("4.3", number) the 4 represents the length of the entire number(including the decimal), not just the part preceding the decimal. The 3 represents the number of decimals. So to get the format correct with up to 4

How would you write a wrapper function or class to format numbers as percent, currency, etc. in R?

纵然是瞬间 提交于 2019-12-03 12:06:32
问题 In a previous question, I asked whether whether a convenient wrapper exists inside base R to format numbers as percentages. This elicited three responses: Probably not. Such a wrapper would be too narrow to be useful. It is better that useRs learn how to use existing tools, such as sprintf , which can format numbers in a highly flexible way. Such a wrapper is problematic, anyway, since you lose the ability to perform calculations on the object. Still, in my view the sprintf function is just a

How do I format my percent variable to 2 decimal places?

痞子三分冷 提交于 2019-12-03 11:03:45
This program is basically working with text files, reading the data & performing functions: while(s.hasNext()){ name= s.next(); mark= s.nextDouble(); double percent= (mark / tm )*100 ; System.out.println("Student Name : " +name ); System.out.println("Percentage In Exam: " +percent+"%"); System.out.println(" "); } I would like to format the percent value to 2 decimal places but since it's inside a while loop I cannot use the printf. Elliot's answer is of course correct, but for completeness' sake it's worth noting that if you don't want to print the value immediately, but instead hold the

how to reduce numbers' significance in JSON's stringify

人走茶凉 提交于 2019-12-03 07:45:45
问题 I have an array with numbers (coordinates) and I want to display those using JSON like so JSON.stringify(array); My array looks like: [{"x":-0.825,"y":0}, {"x":-1.5812500000000003,"y":-0.5625}, {"x":-2.2515625000000004,"y":-1.546875}] But I'm only interested in the first (lets say) 4 significant digits, i.e. [{"x":-0.825,"y":0}, {"x":-1.5813,"y":-0.5625}, {"x":-2.2516,"y":-1.5469}] Is there a way to easily ditch the remaining insignificant numbers? 回答1: Native JSON.stringify accepts the

How to parse numbers more strict than what NumberFormat does in Java?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 07:13:55
I'm validating user input from a form. I parse the input with NumberFormat , but it is evil and allow almost anything. Is there any way to parse number more strict? E.g. I would like to not allow these three inputs, for an integer, but Numberformat allow all of them: NumberFormat nf = NumberFormat.getNumberInstance(); nf.setParseIntegerOnly(true); Number numberA = nf.parse("99.731"); // 99 (not what the user expect) Number numberB = nf.parse("99s.231"); // 99 (invalid) Number numberC = nf.parse("9g9"); // 9 (invalid) System.out.println(numberA.toString()); System.out.println(numberB.toString()

SSRS custom number format

牧云@^-^@ 提交于 2019-12-03 04:56:01
I am go to generate an excel file from SSRS, and I want to format the number like this... 15 is displayed as 15 14.3453453 is displayed as 14.35 12.1 is displayed as 12.1 0 is displayed as 0 1 is displayed as 1 I can apply this in Excel but unable to apply in SSRS [=0]0;[=1]1;0.## Does anyone can suggest another way for me? Thanks! Manish Mishra am assuming that you want to know how to format numbers in SSRS Just right click the TextBox on which you want to apply formatting, go to its expression . suppose its expression is something like below =Fields!myField.Value then do this =Format(Fields

How would you write a wrapper function or class to format numbers as percent, currency, etc. in R?

﹥>﹥吖頭↗ 提交于 2019-12-03 03:32:45
In a previous question , I asked whether whether a convenient wrapper exists inside base R to format numbers as percentages. This elicited three responses: Probably not. Such a wrapper would be too narrow to be useful. It is better that useRs learn how to use existing tools, such as sprintf , which can format numbers in a highly flexible way. Such a wrapper is problematic, anyway, since you lose the ability to perform calculations on the object. Still, in my view the sprintf function is just a little bit too obfuscated for the R beginner to learn (except if they come from a C background).

Localized exponential notation?

扶醉桌前 提交于 2019-12-03 03:14:32
i'm trying to convert numbers into localized strings. For integers and money values it's pretty simple, since the string is just a series of digits and digit grouping separators. E.g.: 12 345 678 901 (Bulgarian) 12.345.678.901 (Catalan) 12,345,678,901 (English) 12,34,56,78,901 (Hindi) 12.345.678.901 (Frisian) 12?345?678?901 (Pashto) 12'345'678'901 (German) i use the Windows GetNumberFormat function to format integers (and GetCurrencyFormat to format money values). But some numbers cannot be reasonably represented in fixed notation, and require scientific notation : 6.0221417930×10 23 or more

how to reduce numbers' significance in JSON's stringify

我怕爱的太早我们不能终老 提交于 2019-12-02 20:30:41
I have an array with numbers (coordinates) and I want to display those using JSON like so JSON.stringify(array); My array looks like: [{"x":-0.825,"y":0}, {"x":-1.5812500000000003,"y":-0.5625}, {"x":-2.2515625000000004,"y":-1.546875}] But I'm only interested in the first (lets say) 4 significant digits, i.e. [{"x":-0.825,"y":0}, {"x":-1.5813,"y":-0.5625}, {"x":-2.2516,"y":-1.5469}] Is there a way to easily ditch the remaining insignificant numbers? Native JSON.stringify accepts the parameter replacer , which can be a function converting values to whatever is needed: a = [0.123456789123456789]