Removing Digits from a Number
Try this:
var num = 1234
var first = num/100
var last = num%100
The playground's output is what you need.
How can I remove a certain number of digits in a number so the number obtained is minimal?
The basic idea is that if you can only remove one digit, you want to remove the first digit (starting with the most significant digit) that is followed by a smaller digit.
For example, if your number is 123432, you want to remove the 4 (since it is followed by a 3), resulting in 12332.
You then repeat this process for as many digits as you want to remove:
char *num = "69469813";
char *buf = malloc(strlen(num)+1);
size_t to_remove = 4;
while (to_remove --> 0) {
char *src = num;
char *dst = buf;
while (*src < *(src+1)) { *dst++ = *src++; } // Advance until the next digit is less than the current digit
src++; // Skip it
while (*dst++ = *src++); // Copy the rest
strcpy(num, buf);
}
printf("%s\n", num); // Prints 4613
How to delete specific digit from integer?
This does not require arrays nor loops:
int n = 1237534;
String newNum = String.valueOf(n);
char c = '3';
StringBuilder sb1 = new StringBuilder( newNum );
StringBuilder sb2 = new StringBuilder( newNum );
int newGuess1 = Integer.parseInt(sb1.deleteCharAt(newNum.indexOf(c)).toString());
int newGuess2 = Integer.parseInt(sb2.deleteCharAt(newNum.indexOf(c, newNum.indexOf(c)+1)).toString());
System.out.println( newGuess1 > newGuess2 ? newGuess1: newGuess2 );
Removing a digit from a number in c++
Here is what you looking for:
#include <bits/stdc++.h>
using namespace std;
int deleteNumber(int num, int n)
{
// Get the length of digits
int d = log10(num) + 1;
// Declare a variable
// to form the reverse resultant number
int rev_new_num = 0;
// Loop with the number
for (int i = 0; num != 0; i++) {
int digit = num % 10;
num = num / 10;
// skip to add if the number is what we need to remove
if (digit == n) {
continue;
}
else {
rev_new_num = (rev_new_num * 10) + digit;
}
}
// Declare a resultant number var
int new_num = 0;
// Loop with the number
for (int i = 0; rev_new_num != 0; i++) {
new_num = (new_num * 10)
+ (rev_new_num % 10);
rev_new_num = rev_new_num / 10;
}
// Return the resultant number
return new_num;
}
// Driver code
int main()
{
int num = 123432111;
// the digit to be deleted
int n = 1;
// Remove the specified digit from starting
cout << deleteNumber(num, n) << endl;
return 0;
}
do while version of it would be:
// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
int deleteNumber(int num, int n)
{
// Get the length of digits
int d = log10(num) + 1;
// Declare a variable
// to form the reverse resultant number
int rev_new_num = 0;
// Loop with the number
do {
int digit = num % 10;
// skip to add if the number is what we need to remove
if (digit == n) {
continue;
}
else {
rev_new_num = (rev_new_num * 10) + digit;
}
} while(num = num / 10);
// Declare a resultant number var
int new_num = 0;
// Loop with the number
for (int i = 0; rev_new_num != 0; i++) {
new_num = (new_num * 10)
+ (rev_new_num % 10);
rev_new_num = rev_new_num / 10;
}
// Return the resultant number
return new_num;
}
// Driver code
int main()
{
int num = 123432111;
// the digit to be deleted
int n = 3;
// Remove the specified digit from starting
cout << deleteNumber(num, n) << endl;
return 0;
}
C++ removing a custom digit from a number
Her is a function which solves your problem
int removeDecimal( int number, int digitNo )
{
int result = number;
int decimal = (int)pow( 10, digitNo ); // caluclate 10.0e<digitNo>
// int decimal = 1;
// for ( int i = 0; i < digitNo; i ++ ) decimal *= 10; // alternative to pow( 10, digitNo )
int div = number / decimal;
if ( div > 0 ) // test if number is grater than 10.0e<digitNo>
{
int rest = number % decimal; // calcualte right digits
result = div / 10; // calcualte left digits
result *= 10 * decimal; // shift left digits to the left
result += rest; // add right digits
}
return result;
}
How to remove a particular digit from an integer using javascript
Try this :
// Input
let number = 789012345;
// Convert number into a string
let numberStr = number.toString();
// Replace the 0 with empty string
const res = numberStr.replace(numberStr[3], '');
// Convert string into a number.
console.log(Number(res));
Removing digits from an integer
Can anyone explain what the error means?
The precedence in your first line is off - infix functions (like /
) usually have a higher precedence than normal ones, so print (...) / 100
is equivalent to (print ...) / 100
, which is obviously problematic. You can wrap everything in brackets, or use the $
function:
print $ ((((i * 5) + 6) * 4 + 9) * 5) / 100 - 1
Now as you've constrained i
to be an Int
, this will still give an error: /
is only defined for instances of the Fractional
typeclass, and Int
isn't. You want integer division, div
or quot
, which operates only on Integral
s (of which Int
is an instance) and perform truncation as you want:
print $ ((((i * 5) + 6) * 4 + 9) * 5) `div` 100 - 1
Note the backticks (`), which allow a normal function to be used infix. You could write this as:
print $ (div ((((i * 5) + 6) * 4 + 9) * 5) 100) - 1
But then the brackets are making things really hard to read.
Are there any other solutions better than truncating by a divisor?
In general probably not, but as mentioned on your other question, this particular equation will always give you the same result as your input, so you can just write:
main = getLine >>= print
How can I remove digits at arbitrary positions?
Converting to a string and removing characters is likely your best bet. Something like the following will work, although it's arguably a bit dense:
removeDigits :: [Int] -> Int -> Int
removeDigits indices x = read . reverse . filterIndices indices . reverse . show $ x
filterIndices :: [Int] -> [a] -> [a]
filterIndices inds elems = map snd . filter ((`notElem` inds) . fst) . zip [1..] $ elems
Note this treats the last digit as the "1st" digit - it's a bit more natural to refer to digits with.
An (in my opinion) easier to read representation of your existing code is:
transform = (subtract 1) . (`quot` 100) . (*5) . (+9) . (*4) . (+6) . (*5)
With this way of writing it, composition overrides the arithmetic precedence laws, letting us write it how it's read ("times 5, add 6, times 4, ..."). We have to use subtract 1
, as -1
is interpreted as the literal value -1
, not as a unary function.
Removing digits from a number but it only works when we choose first digit
The result of substring(a)
is the string from the (a+1)th digit until the end.
You need to break the string in 2 parts.
The 1st part is the string from the start until the digit to be removed (excluded)
and the 2nd part is string after the the digit to be removed .
Then rejoin the 2 strings:
System.out.print("Enter a digit you want to remove: ");
int a;
a = input.nextInt();
String newString = number.substring(0, a - 1) + number.substring(a);
int new_number = Integer.parseInt(newString);
this way you remove the (a+1)th digit.
Related Topics
Local Notification While App Not Running
How to Link Xctest Dependency to Production/Main Target
How to Edit the Uiblureffect Intensity
Class-Only Generic Constraints in Swift
How to Convert Delegate to Observable Rxswift
How to Make a Https Request to a Server in Swift
Changing Color of Button Text and State
Propertywrappers and Protocol Declaration
Multi-Component Picker (Uipickerview) in Swiftui
How to Utilize Nslock to Prevent a Function from Firing Twice
How to Make Apple Sign in Revoke Token Post Request
How to Get All Days in Current Week in Swift
Swift: Unable to Decompose Tuple in Certain Closures (E.G., Reduce with Enumerate)
Remove Multiple Indices from Array
Creating a Custom Scngeometry Polygon Plane with Scngeometryprimitivetype Polygon Crash/Error