Is there a way to multiply any number with integers to get specific digits at specific place values?

2019-04-28 8:56 am
For example, let's say I have the number 0.71631. I want to multiply it with an integer so that way I get a number with .001 within it, the rest of the other digits don't matter. Is there a formula to find an integer or integers that do this without brute forcing it?

回答 (2)

2019-04-28 10:45 am
✔ 最佳答案
Another way to look at the same question is whether any integer multiple of 71631 will have zeros in the thousands and ten-thousands place, and a one in the hundreds place.

This "brute force" method is tedious for hand calculations, but quite easy to write as a computer routine.

Here's a FORTRAN loop:
DO 10 J = 1 TO 9999
N = 71631*J
K = N/100
Comment: the K will simply be a truncation.
IF MOD(K,1000) .EQ. 1 THEN
WRITE (6,*) 'SUCCESS AT ', J, ' ', N
STOP
ENDIF
10 CONTINUE
WRITE(6,*) 'NO LUCK FOR J < 10000'
END
2019-04-28 9:37 am
you are talking about rounding
and or truncating..

0.716 cannot be multiplied by a integer to get less than 0.716

but maybe you mean.. you want 3 digits after the decimal

that is easy

intermediate number = ((number to round) * 10 ^(digits after dec) )+ 0.5

this basically turns the floating point number into a larger number by the amount of the decimals you wish to save ..

you then convert this number into an INTEGER

Integer = intermediate number

then you convert this back to a decimal number

rounded number = ((double) integer) /(10^(digits))

that gives your number rounded

so if you have this number using 3 as digits the steps look like this

intermediate number = 0.71631 * 1000 + 0.5
intermediate number = 716.31 + 0.5 = 716.8

the integer of this is
Integer = 716

then we convert it back

rounded number = 716/1000 = 0.716

NOW this is not completely true because in computers the floating point number is base 2 so it wont be nice and neat all the time but it should be close


收錄日期: 2021-04-24 07:32:53
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20190428005653AAlRXVW

檢視 Wayback Machine 備份