Get the difference in digits between two numbers


Parry

For a simple command line tool, I want to draw a simple graph showing some points and their y-axis values. For the y-axis labels, I want to print the level of the current "row", for example:

55,09|  |
54,90|  ||
54,70|  ||
54,51|  ||
54,32|  ||
54,13|  ||
53,94|  ||
53,75|  ||
53,56|  ||
53,37|  |||
53,18|  |||                   |    |
52,99|  |||            |     ||    |
52,80|  |||         |  |     ||    |
52,61|  |||         || |     |||   |
52,42| ||||||       || |  |  ||||  ||
52,23| ||||||       ||||  |  ||||  ||
52,04| ||||||       ||||  |  |||| |||
51,85| ||||||       ||||  |  |||| |||
51,66| ||||||       |||| ||| |||| |||
51,47| ||||||      ||||||||| ||||||||
51,28| ||||||      ||||||||||||||||||
51,09| ||||||      ||||||||||||||||||
50,90| ||||||     |||||||||||||||||||
50,71| ||||||     |||||||||||||||||||
50,52| |||||||    |||||||||||||||||||
50,33| |||||||    |||||||||||||||||||
50,14| |||||||  |||||||||||||||||||||
49,95| |||||||  |||||||||||||||||||||
49,76| |||||||| |||||||||||||||||||||
49,28| ||||||||||||||||||||||||||||||

But what can happen is that the maximum value has more digits than the minimum value:

1000,00| |
666,67| | |
333,33| |||
0,01|||||

So how can I get the difference in numbers between the max and min values ​​so that leading spaces are added?

1000,00| |
 666,67| | |
 333,33| |||
   0,01|||||
Matthew

Quick and dirty:

double max = getMaximum(); // Get your maximum Y value
int smax = String.format("%.2f", max).length(); // Print as a string and get number of characters

In your loop:

System.out.format("%"+smax+".2f", value);

Edit, from @EJP's comment

In fact, using log10max is both cleaner and more efficient . It will give you a power of 10, so the number of digits (minus one) will be used. Although the first solution is simple (counting characters, which is what we want), this solution is better in every other way:

double max = getMaximum();
int ndigits = (int)Math.floor(Math.log10(max)) + 1;
int precision = 2; // Number of digits after decimal point
String fmt = "%"+(ndigits+1+precision)+"."+precision+"f"; // "%x.pf", x is the TOTAL length, including the point and precision digits

In your loop:

System.out.format(fmt, value);

Related


Javascript function to get the difference between two numbers

Mithun Sreedharan I want a simple Javascript function to get the difference between two numbers in such a way that foo(2, 3)sum foo(3,2) returns the same difference of 1. mykhal: var difference = function (a, b) { return Math.abs(a - b); }

Calculate difference between two numbers and get absolute value

distressed: I want to find the difference between two numbers in Go, the result should not be in "-". Please find my code below: dollarValue := 240000 - 480000 The result is "-240000". But my expected output is only "240000". Can anyone help how to calc

Minimum difference between the sum of two numbers in an array

sky_coder123 I am trying to solve this problem : A physics professor provides projects for students in his class. Students must form a team of two to work on the project. The professor leaves the students to decide the team. The number of students in a class w

Get the difference in digits between two numbers

Parry For a simple command line tool, I want to draw a simple graph showing some points and their y-axis values. For the y-axis labels, I want to print the level of the current "row", for example: 55,09| | 54,90| || 54,70| || 54,51| || 54,32| || 54,13| |

Javascript function to get the difference between two numbers

Mithun Sreedharan I want a simple Javascript function to get the difference between two numbers in such a way that foo(2, 3)sum foo(3,2) returns the same difference of 1. mykhal: var difference = function (a, b) { return Math.abs(a - b); }

Calculate difference between two numbers and get absolute value

distressed: I want to find the difference between two numbers in Go, the result should not be in "-". Please find my code below: dollarValue := 240000 - 480000 The result is "-240000". But my expected output is only "240000". Can anyone help how to calc

Difference between these two ways of finding random numbers?

salmonella For example, if I want to generate random numbers between 5 and 50, I know I can write the code as: ranNum = (int)Math.random()*(50-5+1)+5; or ranNum = Math.round(Math.random()*(50-5))+5; What is the difference between the two in terms of the proc

PHP calculate percentage difference between two numbers

Sanju I am trying to find the percentage difference of two numbers. The reality is that iam gets the total value of user data for this week and last week. Now I need to see the percent difference in performance between this week's data and last week's data. Be

Integer array get difference between two highest numbers

Andy Lixin I have one that List<int[]>I populate by dividing an array of integers into groups of 4. Now I need to get the difference of the two highest numbers in the array. I tried Array.Sort but I'm stuck on how to proceed. What have I done so far? publi

Minimum difference between the sum of two numbers in an array

sky_coder123 I am trying to solve this problem : A physics professor provides projects for students in his class. Students must form a team of two to work on the project. The professor leaves the students to decide the team. The number of students in a class w

Get the difference in digits between two numbers

Parry For a simple command line tool, I want to draw a simple graph showing some points and their y-axis values. For the y-axis labels, I want to print the level of the current "row", for example: 55,09| | 54,90| || 54,70| || 54,51| || 54,32| || 54,13| |

Unable to calculate percentage difference between two numbers

LyonsTheWay2Gold I am trying to display a percentage after subtracting a number. example: Job Cost = £165.00Worker Charges = £42.00Surplus =% What percentage of costs are left after workers take a pay cut? My code output shows 0 int number = 0, number1, result

How to get maximum difference between two numbers in JSON object

Mustafa I'm working with NodeJS to create a "Featured Products" widget on a website. I have a JSON object SKU, priceand sale_price. What is the best way to get SKUthe item with the highest discount (difference between price and sale_price) ? I tried doing this

Calculate difference in digits when comparing two numbers in Lua

self-deception I want to compare two numbers to determine the number of bits that must be flipped in order for them to be equal. For example, 5 and 6 would require a 2-bit flip. I can do this manually, but would like to write a Lua function to do this for me l

Calculate difference in digits when comparing two numbers in Lua

self-deception I want to compare two numbers to determine the number of bits that must be flipped in order for them to be equal. For example, 5 and 6 would require a 2-bit flip. I can do this manually, but would like to write a Lua function to do this for me l

How to find absolute difference between two numbers

User 1145643 What is the best way to find the absolute difference between two numbers in MYSQL so that the results can be sorted? The following works only if numberA is greater than numberB, but as you can see this is not always the case. Is there a good way t

Get percentage difference between two numbers JavaScript

John I need to get the percentage between two numbers like YouTube (likes and dislikes). YouTube gets the number of likes and dislikes to set the percentage of the progress bar, as shown below: I need to do something the same. I have tried doing this but faile

Javascript function to get the difference between two numbers

Mithun Sreedharan I want a simple Javascript function to get the difference between two numbers in such a way that foo(2, 3)sum foo(3,2) returns the same difference of 1. mykhal: var difference = function (a, b) { return Math.abs(a - b); }

Calculate difference between two numbers and get absolute value

distressed: I want to find the difference between two numbers in Go, the result should not be in "-". Please find my code below: dollarValue := 240000 - 480000 The result is "-240000". But my expected output is only "240000". Can anyone help how to calc

Minimum difference between the sum of two numbers in an array

sky_coder123 I am trying to solve this problem : A physics professor provides projects for students in his class. Students must form a team of two to work on the project. The professor leaves the students to decide the team. The number of students in a class w

Get the difference in digits between two numbers

Parry For a simple command line tool, I want to draw a simple graph showing some points and their y-axis values. For the y-axis labels, I want to print the level of the current "row", for example: 55,09| | 54,90| || 54,70| || 54,51| || 54,32| || 54,13| |

Get the difference in digits between two numbers

Parry For a simple command line tool, I want to draw a simple graph showing some points and their y-axis values. For the y-axis labels, I want to print the level of the current "row", for example: 55,09| | 54,90| || 54,70| || 54,51| || 54,32| || 54,13| |

PHP calculate percentage difference between two numbers

Sanju I am trying to find the percentage difference of two numbers. The reality is that iam gets the total value of user data for this week and last week. Now I need to see the percent difference in performance between this week's data and last week's data. Be

How to get maximum difference between two numbers in JSON object

Mustafa I'm working with NodeJS to create a "Featured Products" widget on a website. I have a JSON object SKU, priceand sale_price. What is the best way to get SKUthe item with the highest discount (difference between price and sale_price) ? I tried doing this

How to get maximum difference between two numbers in JSON object

Mustafa I'm working with NodeJS to create a "Featured Products" widget on a website. I have a JSON object SKU, priceand sale_price. What is the best way to get SKUthe item with the highest discount (difference between price and sale_price) ? I tried doing this