PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label digits. Show all posts
Showing posts with label digits. Show all posts

Thursday, August 11, 2022

[FIXED] How can I add 0 before a numeric variable in batch

 August 11, 2022     batch-file, clock, decimal, digits, timer     No comments   

Issue

I can't seem to find the answer anywhere, even tho its such a "basic" thing I would assume someone else had already done it.

Anyways, here goes nothing.

I have made a simple batch script as a timer (counting up) and its in the dd:hh:mm:ss format but only displays what is necessary. For example if days and hours = 0 its only mm:ss etc.

Now I have ran into a slight issue when going from 0-9...

I want it to say 00-01-02-03-04-05... But right now its saying 0-1-2-3-4-5...

Lets say I put the time in a loop, with a 1 second delay always adding one second. I made a numeric /a variable since i'm working with numbers. But I can't seem to figure out how to add a 0 before the number if its less than 10...

So far I've tried the following:

echo %s:""=0%

Nevermind, I figured out the solution while typing this... See my answer


Solution

This is my final code, and it works because I just apply padding at all times if there is room for it.

Simple solution to a simple problem.

@echo off
title Stream counter
color a
cd /d "%~dp0"
set /a x=3
set nl=^&echo.
goto :top
:retry
cls
if %x% LEQ 0 (
    exit()
    ) else (
    title OOPS!
    echo Oops! You either didn't press anything or waited too long,
    echo if you're still there you can try again in a few seconds.
    ping 127.0.0.1 -n 10 >nul
    )
:top
title Stream counter
cls
echo Attemps left^: %x%
echo Start counter from zero? (y/n)
choice /n /c YNC /t 10 /d C>nul
if %errorlevel%==1 (
    goto :yes
    ) else if %errorlevel%==2 (
    goto :no
        ) else if %errorlevel%==3 (
        set /a x-=1
        goto :retry
        )
:yes
cls
set /a d=0
set /a h=0
set /a m=0
set /a s=0
goto :start
:no
cls
echo where do you want your timer to start?
echo Start by entering day^(s^)
set /p d=""
echo Now enter hour^(s^)
set /p h=""
echo Then enter minute^(s^)
set /p m=""
echo And finally second^(s^)
set /p s=""

goto :start
:start
rem delete this if you want to keep the result (it will get overwritten anyways...)
del final.txt
rem Use the choice to ask if you want to continue, and use that as a "Press any key to stop", kind of thing.
:startloop
::Check point "seconds"
:cps
if %s% geq 60 (
set /a s-=60
set /a m+=1
goto :startloop
)
::Check point "minutes"
:cpm
if %m% geq 60 (
set /a m-=60
set /a h+=1
goto :startloop
)
::Check point "hours"
:cph
if %h% geq 24 (
set /a h-=24
set /a d+=1
goto :startloop
) else (
goto :mainloop
)
:mainloop
cls
set ps=00%s%
set pm=00%m%
set ph=00%h%
set ps=%ps:~-2%
set pm=%pm:~-2%
set ph=%ph:~-2%
if %d% geq 1 (
    if %h% geq 1 set "output=%d% day^(s^) %ph%^:%pm%^:%ps%"
    if %h% equ 0 set "output=%d% day^(s^) %pm%^:%ps%"
) else if %h% geq 1 (
    set "output=%ph%^:%pm%^:%ps%"
) else (
    set "output=%pm%^:%ps%"
)
echo %output% > counter.txt
echo Current time^: %output% &echo.
echo Do you wish to abort? (Y/N)
choice /n /c yn /t 1 /d n >nul
if %errorlevel%==1 goto :end
set /a s+=1
::Check point "seconds"
:cps
if %s% geq 60 (
set /a s-=60
set /a m+=1
)
::Check point "minutes"
:cpm
if %m% geq 60 (
set /a m-=60
set /a h+=1
)
::Check point "hours"
:cph
if %h% geq 24 (
set /a h-=24
set /a d+=1
)
goto :mainloop
:end
:: END ::
rem COPY TO RESULT.txt and del counter.txt
copy /V counter.txt final.txt & del counter.txt
exit()


Answered By - tsgsOFFICIAL
Answer Checked By - Timothy Miller (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Sunday, August 7, 2022

[FIXED] How to print decimal notation rather than scientific notation in Python?

 August 07, 2022     decimal, digits, formatting, python, scientific-notation     No comments   

Issue

Assume to have this number:

p=0.00001

Now, if we print this variable we get 1e-05 (i.e., that number in scientific notation). How can I print exactly 0.00001 (i.e., in decimal notation)? Ok, I know I can use format(p, '.5f'), but the fact is that I don't know in advance the number of decimal digits (e.g., I may have 0.01 or 0.0000001, etc.). Therefore, I could count the number of decimal digits and use that in the format function. In doing so, I have a related problem... is there any way to count the decimal digits of a decimal number without using Decimal?


Solution

p = 0.0000000000000000000001

s = str(p)

print(format(p, "." + s.split("e")[-1][1:]+"f")) if "e" in s else print(p)


Answered By - Padraic Cunningham
Answer Checked By - David Goodson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Saturday, August 6, 2022

[FIXED] How to round a number to n decimal places in Java

 August 06, 2022     decimal, digits, java, rounding     No comments   

Issue

What I would like is a method to convert a double to a string which rounds using the half-up method - i.e. if the decimal to be rounded is 5, it always rounds up to the next number. This is the standard method of rounding most people expect in most situations.

I also would like only significant digits to be displayed - i.e. there should not be any trailing zeroes.

I know one method of doing this is to use the String.format method:

String.format("%.5g%n", 0.912385);

returns:

0.91239

which is great, however it always displays numbers with 5 decimal places even if they are not significant:

String.format("%.5g%n", 0.912300);

returns:

0.91230

Another method is to use the DecimalFormatter:

DecimalFormat df = new DecimalFormat("#.#####");
df.format(0.912385);

returns:

0.91238

However as you can see this uses half-even rounding. That is it will round down if the previous digit is even. What I'd like is this:

0.912385 -> 0.91239
0.912300 -> 0.9123

What is the best way to achieve this in Java?


Solution

Use setRoundingMode, set the RoundingMode explicitly to handle your issue with the half-even round, then use the format pattern for your required output.

Example:

DecimalFormat df = new DecimalFormat("#.####");
df.setRoundingMode(RoundingMode.CEILING);
for (Number n : Arrays.asList(12, 123.12345, 0.23, 0.1, 2341234.212431324)) {
    Double d = n.doubleValue();
    System.out.println(df.format(d));
}

gives the output:

12
123.1235
0.23
0.1
2341234.2125

EDIT: The original answer does not address the accuracy of the double values. That is fine if you don't care much whether it rounds up or down. But if you want accurate rounding, then you need to take the expected accuracy of the values into account. Floating point values have a binary representation internally. That means that a value like 2.7735 does not actually have that exact value internally. It can be slightly larger or slightly smaller. If the internal value is slightly smaller, then it will not round up to 2.7740. To remedy that situation, you need to be aware of the accuracy of the values that you are working with, and add or subtract that value before rounding. For example, when you know that your values are accurate up to 6 digits, then to round half-way values up, add that accuracy to the value:

Double d = n.doubleValue() + 1e-6;

To round down, subtract the accuracy.



Answered By - curtisk
Answer Checked By - Senaida (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Wednesday, July 20, 2022

[FIXED] How to ascending order the following integers with maple (should not display any digit)

 July 20, 2022     digits, display, integer, maple     No comments   

Issue

In this exercise you should not display any digit. Display only the minimal output required to establish your result. (a) Place the following integers in ascending order 100^(100); 80^(120); 60^(140); 40^(160)

evalb(-3<-2)

anyone can help what's next?


Solution

It's unclear whether you have to show it by steps, or whether you can simply get Maple to sort the quantities directly.

Consider these three comparisons, which should be enough for you to figure out the ascending order:

evalb( 80^(120) > 100^(100) );

           true

evalb( 60^140 > 80^(120) );

           true

evalb( 40^(160) > 60^140 );

           true

Or, starting with a random rearrangement, you can get Maple to sort them.

restart;

# separate paragraph or execution group
interface(typesetting=extended):

S := [100%^(100), 40%^(160), 60%^(140), 80%^(120) ];

answer := sort(S, (a,b)->value(a)<value(b));


Answered By - acer
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Monday, May 9, 2022

[FIXED] How do i write a program that calculates the product of a number's digits that are different from another number i input?

 May 09, 2022     c++, digits, product     No comments   

Issue

I have to input 2 numbers, n and k, and find the product of n's digits which are different from k. Example: n = 1234, k=2, and the product should be 1 * 3 * 4;

    #include <iostream>

using namespace std;

int main()
{
    int n, k, p=1, digit;
    cin >> n >> k;

    while(true){
            digit= n % 10;

        if(digit != k){
            p = p * digit;
        }
        n = n/10;
    }
    cout << "Product of digits different from K is: " << p;
    return 0;
}

When i run it, after i input n and k, the program doesnt do anything else and just keeps the console open without an output.


Solution

The problem is the while(true). In this way the program stay in the loop for eternal. A possible solution can be this:

int main()
{
    int n, k, p=1, digit;
    cin >> n >> k;

    while( n > 0 ){
            digit= n % 10;

        if(digit != k){
            p = p * digit;
        }
        n = n/10;
    }
    cout << "Product of digits different from K is: " << p;
    return 0;
}


Answered By - Zig Razor
Answer Checked By - Cary Denson (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing